逻辑问题 - 数字求和
我需要一些关于程序逻辑的帮助。我想做的是:
写一个程序,用循环让用户输入一系列正数。用户输入一个负数来表示结束这个系列。在用户输入完所有正数后,程序应该显示这些数字的总和。
keep_going = ' '
max = ()
total = 0.0
print('This program will add numbers together until a negative number is entered.')
print('It will then show the total of the numbers entered.')
while keep_going != (-):
number = int(input('Enter a number: '))
total = total + number
print('The total is', total)
我哪里出错了?
2 个回答
0
你从来没有改变过 keep_going
的值,所以你的循环永远不会结束。
5
使用一个无限循环,然后检查刚输入的数字是否小于0:
total = 0
while True:
number = int(input('Enter a number: '))
if number < 0:
break
total = total + number
只有通过检查刚输入的数字,你才能发现什么时候输入了一个负数。