2017-09-22 95 views
0

我想写一个程序,要求输入两个数字,然后通过运行打印总和,产品和平均值。我写了一个程序,但它每次需要输入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)) 

回答

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)) 
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

你会想先拿到号码,然后做你对它们的操作。否则你依靠用户送花儿给人输入相同的两个数字:

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))