【发布时间】:2015-09-13 19:55:53
【问题描述】:
我是编程新手,我想知道如果用户输入无效数据,我该如何重复输入部分。
我希望应用程序只重复输入部分,而不是重新运行函数并让用户重新输入所有内容。
我的猜测是我必须将“return main()”更改为其他内容。
condition = input("What is the condition of the phone(New or Used)?")
if condition not in ["New", "new", "Used", "used"]:
print("Invalid input")
return main()
gps = input("Does the phone have gps(Yes or No)?")
if gps not in ["Yes", "yes", "No", "no"]:
print("Invalid input")
return main()
【问题讨论】:
-
看看while loop。注意:在示例中,它使用
raw_input(),即python 2.7。如果你有python 3.4,你应该使用input()