Python代码没有循环回到顶部
在我的编程课上,我们必须随机编写一个简单的文本冒险风格的代码。我可以完成基本操作,但当我尝试在末尾添加重试时,它只会询问用户是否希望反复重试,而不是返回到代码的开头Python代码没有循环回到顶部,python,loops,while-loop,Python,Loops,While Loop,在我的编程课上,我们必须随机编写一个简单的文本冒险风格的代码。我可以完成基本操作,但当我尝试在末尾添加重试时,它只会询问用户是否希望反复重试,而不是返回到代码的开头 import random def main(): cave1=('text1') cave2=('text2') #Radom assignment for caves checkCave=random.randint(1,2) #Text of situation dis
import random
def main():
cave1=('text1')
cave2=('text2')
#Radom assignment for caves
checkCave=random.randint(1,2)
#Text of situation
displayIntro='Two caves, one good, one bad'
print(displayIntro)
#input for cave choice.
chooseCave=int(input('Which cave do you enter?: (enter a 1 or 2): '))
while chooseCave<1 or chooseCave>2:
print('Error: please enter a 1 or a 2.')
print()
#outcome
print('You slowly walk towards the cave.')
print()
if checkCave==1:
print(cave1)
else:
print(cave2)
print()
again='y'
while True:
while True:
again=input('Do you want to try again? y for yes, n to quit: ')
if again in ('y','n'):
break
print('Invalid input.')
if again=='y':
continue
else:
print('Goodbye')
break
return again
main()
任何帮助都将不胜感激。
谢谢 continue仅表示开始for循环的下一次迭代,或在中找到for循环时开始下一次迭代。如果要循环外部代码,则需要外部for循环
将东西放入函数中可能有助于使事情变得更容易
祝你好运 继续是导致问题的原因