【发布时间】:2015-08-19 23:23:01
【问题描述】:
在下面的代码中,为什么打架设置为False时不停止循环?
我知道它不会停止循环,因为当战斗设置为 False 时它不会进入战利品部分。这是整个while循环:
while fighting:
cls()
print("The enemy has", opponent.HP, "HP!")
input()
if int(opponent.HP) <= 0:
print("Yep yep")
winner = True
fighting = False
elif int(ownedCreatures[activeCreature].HP) <= 0:
winner = False
fighting = False
showFight(opponent, activeCreature)
allowed = ["a", "i", "r"]
choice = input(">>")
while not choice in allowed:
choice = input("Try again please >>")
if choice.lower() == "a":
if previousTurn == "Not defined":
num = random.randint(1, ownedCreatures[activeCreature].support + opponent.support)
if num <= ownedCreatures[activeCreature].support:
attacker = "player"
previousTurn = "player"
else:
attacker = "opponent"
previousTurn = "opponent"
else:
if previousTurn == "player":
attacker = "opponent"
previousTurn = "opponent"
else:
attacker = "player"
previousTurn = "player"
attack(attacker, activeCreature, opponent)
#if choice.lower() == "i":
if choice.lower() == "r":
num = random.randint(1, ownedCreatures[activeCreature].support + opponent.support)
if num <= ownedCreatures[activeCreature].support:
cls()
print("-------------------------------------------")
print("You succesfully escaped this horrible fight!")
print("-------------------------------------------\n")
input("Press Enter to continue... >> ")
winner = "Not defined"
fighting = False
else:
cls()
print("-------------------------------------------")
print("Think you can run that easily?")
print("-------------------------------------------\n")
input("Press Enter to continue... >> ")
#After the fight
if winner == True:
cls()
loot()
elif winner == False:
cls()
print("-------------------------------------------")
print("You have lost the fight!")
print("You lost 50 Serra!")
serra = serra - 50
if serra < 0:
serra = 0
print("-------------------------------------------\n")
input("Press Enter to continue... >> ")
【问题讨论】:
-
你怎么知道它不会停止?这就是你在
while循环中所做的一切吗? -
你能发个MCVE吗?
-
请打印
opponent.HP的值 -
@RicardoFrederiks 你能打印这些值吗:
opponent.HP,ownedCreatures[activeCreature].HP,ownedCreatures[activeCreature].support + opponent.support -
ownedCreatures[activeCreature].support,对手.support
标签: python loops while-loop break