python中的总和/平均值/乘积

时间:2017-09-22 20:00:54

标签: python sum average product

我正在尝试编写一个程序,要求输入两个数字,然后运行它来打印总和,产品和平均值。我写了一个程序,但每当我需要一个总和或平均值或产品时,它会询问输入2个数字。如何一次获得所有3个,只需输入两个输入。

sum = int(input("Please enter the first Value: ")) + \
      int(input("Please enter another number: "))
print("sum = {}".format(sum))

product = int(input("Please enter the first Value: ")) * \
          int(input("Please enter another number: "))
print ("product = {}".format(product))

3 个答案:

答案 0 :(得分:1)

您需要将数字分配给变量,然后将其重复用于操作。

示例

x = int(input("Please enter the first Value: "))
y = int(input("Please enter another number: "))

print("sum = {}".format(x+y))
print("product = {}".format(x*y))
print("average = {}".format((x+y)/2))

答案 1 :(得分:1)

你想要先获得数字,然后对它们进行操作。否则,你依靠用户总是输入相同的两个数字:

a = int(input("Please enter the first Value: "))
b = int(input("Please enter the second Value: "))

print ("sum = {}".format(a+b))
print ("product = {}".format(a*b))
print ("average = {}".format((a*b)/2))

答案 2 :(得分:1)

使用变量存储输入:

first_number = int(input("Please enter the first Value: "))
second_number = int(input("Please enter another number: "))

sum = first_number + second_number
product = first_number * second_number
average = (first_number + second_number) / 2

print('Sum is {}'.format(sum))
print('product is {}'.format(product))
print('average is {}'.format(average))