簡體   English   中英

為什么我不能跳出循環? (蟒蛇)

[英]Why can't I break out of my loop? (Python)

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

您不需要兩個循環,只需一個循環。

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

    ...

您的代碼只是打破了內部循環,而不是外部循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM