所以我搜索了“string”、“python”、“validate”、“user input”等词的每一个排列,但还没有找到一个适合我的解决方案。
我的目标是提示用户是否要使用字符串“yes”和“no”启动另一个事务,我认为在Python中,字符串比较是一个相当简单的过程,但有些事情并不正常。我使用的是Python3.X,因此据我所知,输入应该是在不使用原始输入的情况下接受字符串。
即使在输入“是”或“否”时,程序始终会回退无效输入,但真正奇怪的是,每次输入长度为4个字符或int值的字符串时,它都会将其作为有效的正输入进行检查,然后重新启动程序。我找不到有效的否定输入。endProgram = 0;
while endProgram != 1:
#Prompt for a new transaction
userInput = input("Would you like to start a new transaction?: ");
userInput = userInput.lower();
#Validate input
while userInput in ['yes', 'no']:
print ("Invalid input. Please try again.")
userInput = input("Would you like to start a new transaction?: ")
userInput = userInput.lower()
if userInput == 'yes':
endProgram = 0
if userInput == 'no':
endProgram = 1
我也试过while userInput != 'yes' or userInput != 'no':
我将非常感谢您不仅帮助我解决问题,而且如果有人有任何关于Python如何处理字符串的额外信息,那将是非常好的。
如果有人已经问过这样的问题,我会事先道歉,但我已经尽力去搜索了。
谢谢大家!
~戴夫