在我的inputCheck函数中,当用户输入被检查后通过时,是否应该通过打印消息确认可接受的输入然后运行另一个函数 - 但是它没有这样做,我无法弄清楚为什么 - 会你能就如何解决这个问题提出建议吗?非常感谢!
def main():
print('WELCOME TO THE WULFULGASTER ENCRYPTOR 9000')
print('==========================================')
print('Choose an option...')
print('1. Enter text to Encrypt')
print('2. Encrypt text entered')
print('3. Display Encrypted Text!')
menuChoice()
def menuChoice():
valid = ['1','2','3']
userChoice = str(input('What Would You Like To Do? '))
if userChoice in valid:
inputCheck(userChoice)
else:
print('Sorry But You Didnt Choose an available option... Try Again')
menuChoice()
def inputCheck(userChoice):
if userChoice == 1:
print('You Have Chosen to Enter Text to Encrypt!')
enterText()
if userChoice == 2:
print('You Have Chosen to Encypt Entered Text!')
encryptText()
if userChoice == 3:
print('You Have Chosen to Display Encypted Text!')
displayText()
def enterText():
print('Enter Text')
def encryptText():
print('Encrypt Text')
def displayText():
print('Display Text')
main()
答案 0 :(得分:3)
您将用户的输入转换为字符串(str(input('What ...'))
),但将其与inputCheck
中的整数进行比较。由于else
中没有inputCheck
路径,因此当您输入“有效”选项时,没有任何操作。
此外,如果你使用的是Python 2,那么使用input
并不是你想要的,raw_input
就是你要去的地方(例如,参见What's the difference between raw_input() and input() in python3.x?)。
除此之外,每当用户输入非法选择时递归调用menuChoice
可能是一个坏主意:输入几百或一千次的非法选择,你的程序将崩溃(除了浪费很多记忆)。你应该把代码放在循环中:
while True:
userChoice = str(raw_input('What Would You Like To Do? '))
if userChoice in valid:
inputCheck(userChoice)
break
else:
print('Sorry But You Didnt Choose an available option... Try Again')