[英]Why can't I break out of my loop? (Python)
import rando
move = rando.Moves()
All of this junk is just writing to a file to give some context about the game 所有这些垃圾只是写入文件以提供有关游戏的一些上下文
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
A buddy of mine told me I had to have two while loops so not too sure why I need those but it works slightly like it's meant to. 我的一个伙伴告诉我,我必须有两个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) "))
The problem is here. 问题在这里。 I have a module that I made that if the attack fails, you die and player gets set to False but it still continues the while loop.
我有一个模块,如果攻击失败,您将死亡并且玩家设置为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
You don't need two loops, just one. 您不需要两个循环,只需一个循环。
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
...
Your code was just breaking out of the inner loop, not the outer loop. 您的代码只是打破了内部循环,而不是外部循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.