【发布时间】:2014-01-30 18:16:30
【问题描述】:
我试图在 python 上多次获取用户输入,然后取其总和。
多次获取用户输入意味着我每次都需要一个新变量来存储用户输入。我需要一个可以接受无限用户输入的程序,当然不可能将新变量分配给无限用户输入并将它们相加。 python中有没有可以自己不断添加值的内置函数?
这是我的代码。由于哨兵,它没有给我总和(我不明白为什么)。 请帮忙。
var = int(raw_input("Enter 1,2,3 or 4 for add,subtract,multiplication,division respectively: "))
if var == 1:
print "You chose to add.Lets add!! :)"
def main ():
total = 0.0
while True:
number = int(raw_input('enter a number: '))
if number == 0:
total+=number
break
print 'the total is', total
主()
【问题讨论】:
标签: python python-2.7 loops calculator infinite