简体   繁体   English

有没有其他方法可以做到这一点,或者我错过了什么

[英]is there a another way to do this or i am missing something

Question问题

Given two integers a and b, your task is to calculate:给定两个整数 a 和 b,你的任务是计算:

  • a+b a+b
  • a- b a-b
  • a*b a*b
  • a//b a//b

Input输入

User Task: Since this will be a functional problem, you don't have to take input.用户任务:由于这将是一个功能问题,因此您不必接受输入。 You just have to complete the function operations() that takes the integer a and b as parameters.您只需完成以 integer a 和 b 作为参数的 function 操作()。

Constraints:约束:

  • 1 <= b <= a <= 1000 1 <= b <= a <= 1000

Note: It is guaranteed that a will be divisible by b.注意:保证 a 可以被 b 整除。

Output Output

Print the mentioned operations each in a new line.在新行中打印每个提到的操作。

This is my code:这是我的代码:

def operations(X,Y):
  return X+Y

def minus(X,Y):
  return X-Y

def multi(X,Y):
  return X*Y

def div(X,Y):
  return X/Y

X = 15
Y = 3

print(minus(X,Y))
print(operations(X,Y))
print(multi(X,Y))
print(div(X,Y))

It would be a huge help if someone tells me if the code I am doing has something wrong with it.如果有人告诉我我正在做的代码是否有问题,那将是一个巨大的帮助。

if you use (/) 11/3 in division then it will return float value (3.666666...) if you use(//) 11//3 it will give you a rounded int value (3)如果你在除法中使用 (/) 11/3 那么它将返回浮点值 (3.666666...) 如果你使用 (//) 11//3 它会给你一个四舍五入的 int 值 (3)

I don't think the problem asks you to add the functions minus , multi and div .我不认为问题要求您添加函数minusmultidiv You only need to implement the function operations and make print the result of the four operation each in one line.您只需执行 function operations并将四个操作的结果分别打印在一行中。 Which can be done like this:可以这样做:

def operations(a, b):
  print(a + b)
  print(a - b)
  print(a * b)
  print(a // b)

Notice how I used a double slash ( // ) for division instead of a single slash ( / ).请注意我是如何使用双斜杠 ( // ) 而不是单斜杠 ( / ) 进行除法的。 Because a single slash in python will give you a float value even though a and b are integers.因为 python 中的单个斜杠会给你一个浮点值,即使ab是整数。 ie. IE。 4 / 2 will return 2.0 where the problem is clearly asking for integer values (cf. the note), in this case 2 4 / 2将返回2.0问题显然是要求 integer 值(参见注释),在这种情况下为2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM