为什么我不能摆脱循环? (蟒蛇)

时间:2018-06-06 16:32:47

标签: python loops while-loop

import rando

move = rando.Moves()

所有这些垃圾只是写入文件以提供有关游戏的背景信息

oof = open('README.txt', 'w') #opens the text file
oof.write("The Final Adventure\n")
oof.write("\n") #writes text to the file
oof.write("This game is about an adventurer(you) trying to fight through a 
dungeon in order to get the legendary sword of mythos.\n")
oof.write("\n")
oof.write("With this legendary sword you can finally rid your homeland of 
the evil king of Mufasa and take the mantle of kingship.\n")
oof.write("\n")
oof.write("Best of luck to you, Warrior!\n")
oof.write("\n")
oof.write("You have 3 move options with any enemy you encounter.\n")
oof.write("\n")
oof.write("1 - Attack\n")
oof.write("2 - Run Away\n")
oof.write("3 - Talk\n")
oof.write("Have fun!!")
oof.close() #closes the file

print("Please read the README.txt in this file")

player = True

我的一个伙伴告诉我,我必须有两个while循环,所以不太确定我为什么需要这些,但它的工作方式有点像它的意思。

while True:
    while player:
        #First Door
        print("You walk through the forest until you finally find the talked 
        about entrance door.")
        print("There is a Gatekeeper standing by the entrance and in order 
        to enter you must defeat him")
        print("")
        move = int(input("What will you do?(Please use numbers 1-2-3 for 
        moves) "))

问题出在这里。我有一个模块,如果攻击失败,你死了,玩家被设置为False,但它仍然继续while循环。

        if move == 1:
            rando.Moves.move1(player)
        elif move == 2:
            rando.Moves.move2(player)
        elif move == 3:
            rando.Moves.move3(player)
        else:
            print("You ran out of time and died")
            player = False

        if player == False:
           break

        #First Room
        print("The door suddenly opens and you walk through.")
        print("")
        print("After walking for a bit you discover a chest, probably full 
        of loot!")
        print("You walk up and begin to open the chest and it clamps down on 
        your hand with razor sharp teeth.")
        print("ITS A MONSTER!")
        print("After struggling, you are able to free your hand.")
        move = int(input("What will you do? "))
        if move == 1:
            rando.Moves.move1(player)
        elif move == 2:
            rando.Moves.move2(player)
        elif move == 3:
            rando.Moves.move3(player)
        else:
            print("You ran out of time and died")
            player = False

    #Play again
    play = input("Want to play again? (y/n) ")
    if play == "y":
        player = True
    elif play == "n":
        print("Goodbye!")
        break

1 个答案:

答案 0 :(得分:1)

您不需要两个循环,只需一个。

while True:
    if move == 1:
        rando.Moves.move1(player)
    elif move == 2:
        rando.Moves.move2(player)
    elif move == 3:
        rando.Moves.move3(player)
    else:
        print("You ran out of time and died")
        break

    ...

你的代码只是打破了内循环,而不是外循环。

相关问题