[英]How to go back to a list of choices?
所以我是 python 的新手。 只是想知道,是否可以在 c++ 中使用 goto 之类的东西。
我做出这样的选择
print("1. choice")
print("2. choice")
print("3. choice")
choiceInput = input("Input your choice")
if choiceInput == 1:
print("Bla bla bla")
在他选择了 1 个选项并获得了他选择的内容之后,我如何让他重新能够再次从所有 3 个选项中进行选择? 谢谢,对 python 很陌生,刚刚学习如何制作基本的 cmd 应用程序。
您需要某种循环:
while True:
print("1. choice")
print("2. choice")
print("3. choice")
choiceInput = input("Input your choice")
if choiceInput == 1:
print("Bla bla bla")
使用循环,再次执行代码
print("1. choice")
print("2. choice")
print("3. choice")
print("4. STOP")
while True:
choiceInput = int(input("Input your choice")) # don't forget int conversion
if choiceInput == 1:
print("Bla bla bla")
elif choiceInput == 4:
break
使用 while 循环不断重复
while True:
print("1. choice")
print("2. choice")
print("3. choice")
choiceInput = input("Input your choice")
if int(choiceInput) == 1: # Make sure to convert it to integer otherwise it will check between int and str
print("Bla bla bla")
这将导致它在完成后重复。 请参阅有关while 循环的 python wiki
def choice () :
print("1. choice")
print("2. choice")
print("3. choice")
choiceInput = input("Input your choice")
return choiceInput
while True :
choiceInput = choice()
if choiceInput == "1":
print("Bla bla bla")
默认input
返回一个字符串,不要忘记先进行int conversion
:
choiceInput = int(input("Input your choice"))
您可以使用带有一个中断条件的 while 循环。
代码可以如下:
while True:
print("1. choice")
print("2. choice")
print("3. choice")
print("4. Exit.")
choiceInput = int(input("Input your choice"))
if choiceInput == 1:
#code for 1.
elif choiceInput == 2:
#code for 2.
elif choiceInput == 3:
#code for 3.
elif choiceInput == 4:
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.