所以我得到的输入存储到用户的列表中,我正在使用一个哨兵循环来不断要求用户输入一个数字。出现的问题是,当用户输入值时,我使用“停止”结束循环我得到一个错误,这是
ValueError:int()的基数为10的无效文字:'停止'
我不知道为什么,如果是因为它输入一个字符串来结束输入是用于整数的while循环。任何建议摆脱这个错误非常感谢谢谢,我的代码也在下面。
def getInput():
nums = []
print("Enter a value, to end the list, input Stop")
userInput = input("")
while userInput.upper() != "Stop":
print("Enter a value, to end the list, input Stop")
nums.append(int(userInput))
userInput = input("")
return nums
def main():
numbers = getInput()
print(numbers)
main()
userInput.upper() != "Stop":
永远是True
:'stop'.upper()
是'STOP'
。
如果你想让你的循环终止,当用户输入任何大写版本的'stop'
你应该写
while userInput.upper() != "STOP":
....
捕捉用户可以输入的其他东西可能是明智的
userIntput = input("")
try:
nums.append(int(userInput))
except ValueError:
# somehow handle what should happen here...