所以我的代码不断显示数字的总和10次,而不是一次。我试图让用户输入尽可能多的用户输入,但是一旦输入负整数,我需要它停止。一旦发生这种情况,它应该停止让用户添加数字并仅显示正数的总和。请告诉我这里我做错了什么。我对编程很新,而且我被卡住了。
max = 10
total = 0.0
positive_num = 0
for counter in range (max):
while positive_num > -1:
total = total + positive_num
positive_num = int(input('Enter a positive number: '))
else:
print('Total of positive numbers is:', total)
你的循环不是让用户输入任意数量,它只输入最多10个数字,你应该将它改为无限循环,并在负输入上中断:
total = 0.0
while True:
positive_num = int(input('Enter a positive number: '))
if positive_num > -1:
total = total + positive_num
else:
print('Total of positive numbers is:', total)
break