在上面的代码中,当我按下任何其他键时,我希望在返回 user_input 之前弹出一条错误消息。请帮助某人?
问问题
70 次
2 回答
2
while
在循环结束时检查它是否有效:
currencies = ['pound', 'euro', 'dollar', 'yen', '$', '£', '¥','€']
def get_currency(msg):
while True:
user_input = input(msg).lower()
if user_input not in currencies:
print('Invalid currency')
else:
return user_input
于 2013-04-17T18:15:46.637 回答
2
# -*- coding: utf-8 -*-
valid = ['pound', 'euro', 'dollar', 'yen', '$', '£', '¥','€']
msg="Enter currency: "
def get_currency(msg):
input_valid = input(msg).lower()
while input_valid not in valid: #run this loop until the input is not valid
print ("Invalid Input, Try again") #the error message
input_valid = input(msg).lower()
return input_valid
print (get_currency(msg))
输出:
~$ python3 so.py
Enter currency: foo
Invalid Input, Try again
Enter currency: bar
Invalid Input, Try again
Enter currency: pound
pound
于 2013-04-17T18:18:10.583 回答