Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用2个循环,如果为false,则不间断地重复程序_Python_Loops - Fatal编程技术网

Python 如何使用2个循环,如果为false,则不间断地重复程序

Python 如何使用2个循环,如果为false,则不间断地重复程序,python,loops,Python,Loops,现在我正在这个网站上做“龙王国”教程 我稍微了解这一切,但这不是我的问题。最后,我想添加一段代码,如果玩家说不,它会说,就像我现在看到的那样,“太糟糕了”*!”并返回到程序的开头。我已经让它这样做了,但一旦它通过第二次,我得到的输入,你是否想再试一次,无论你是否键入是或否,它只是结束程序。我尝试了while、if/else、while True、while False的多种组合,但没有得到我想要的结果。我不明白你怎么能坚持下去?这可能真的很简单,但我想不出来 这是程序的代码 import ran

现在我正在这个网站上做“龙王国”教程

我稍微了解这一切,但这不是我的问题。最后,我想添加一段代码,如果玩家说不,它会说,就像我现在看到的那样,“太糟糕了”*!”并返回到程序的开头。我已经让它这样做了,但一旦它通过第二次,我得到的输入,你是否想再试一次,无论你是否键入是或否,它只是结束程序。我尝试了while、if/else、while True、while False的多种组合,但没有得到我想要的结果。我不明白你怎么能坚持下去?这可能真的很简单,但我想不出来

这是程序的代码

import random
import time
def displayIntro():
    print('You are in a land full of dragons. In front of you,')
    print('you see two caves. In one cave, the dragon is friendly')
    print('and will share his treasure with you. The other dragon')
    print('is greedy and hungry, and will eat you on sight.')
    print()
def chooseCave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave will you go into? (1 or 2)')
        cave = input()
    return cave
def checkCave(chosenCave):
    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)
    print('A large dragon jumps out in front of you! He opens his jaws and...')
    print()
    time.sleep(2)
    friendlyCave = random.randint(1, 2)
    if chosenCave == str(friendlyCave):
        print('Gives you his treasure!')
    else:
        print('Gobbles you down in one bite!')
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
    displayIntro()
    caveNumber = chooseCave()
    checkCave(caveNumber)
    print('Do you want to play again? (yes or no)')
    playAgain = input()
你可以简单地加上

if 'n' in playAgain:
    print "too bad"
    playAgain = 'yes'
在末尾(在while循环中)

顺便说一下,这两条线可以结合起来:

print('Do you want to play again? (yes or no)')
playAgain = input()
简单地说:

playAgain = input('Do you want to play again? (yes or no)')
因为请求输入时,
input
将显示字符串参数。

您可以简单地添加

if 'n' in playAgain:
    print "too bad"
    playAgain = 'yes'
在末尾(在while循环中)

顺便说一下,这两条线可以结合起来:

print('Do you want to play again? (yes or no)')
playAgain = input()
简单地说:

playAgain = input('Do you want to play again? (yes or no)')

因为请求输入时,
input
将显示字符串参数。

不需要脏话,请将代码减少到问题的关键部分。请原谅我在代码中发布了我的内容,但请注意,尽管您的审查制度更冒犯我。安德烈,问题已经解决,但是,谢谢你关心帮助我!不需要粗俗的语言请将代码减少到您问题的关键部分。请原谅我在代码中发布了我的内容,但请注意,尽管您的审查制度更冒犯我。Andre问题已经解决,但是,感谢您对我的帮助!