在我的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()
将用户的输入转换为字符串(str(input('What ...'))
),但将其与inputCheck
中的整数进行比较。由于inputCheck
中没有else
路径,所以当您输入"有效"选择时,什么也不会发生。
另外,如果你正在使用Python 2,使用input
不是你想要的,raw_input
是一种方法(例如,请参阅python3.x中raw_input()和input()之间的区别是什么?)。
除此之外,每当用户输入非法选项时,递归地调用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')