Почему я не могу вырваться из своей петли?(Python) - PullRequest
0 голосов
/ 06 июня 2018
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 голосов
/ 06 июня 2018

Вам не нужны два цикла, только один.

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

    ...

Ваш код просто разрывался из внутреннего цикла, а не из внешнего цикла.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...