我的代码陷入了一个无限循环,我需要帮助修复它。 每当玩家(我)杀死怪物(pc),它就会陷入无限循环。你知道吗
我试着修补我的凹痕,但没用。你知道吗
while enemyHealth > 0:
enemyAttack = random.choice (['rock', 'paper', 'scissors'])
playerAttackKey = input("CHOOSE YOUR WEAPON! Rock (R), Paper (P) or Scissors (S). Type the letter to initiate your attack! Take note that each of your succesful attacks do 1 DMG! ")
if playerAttackKey == 'R' or playerAttackKey == 'r':
playerAttack = 'Rock'
elif playerAttackKey == 'P' or playerAttackKey == 'p':
playerAttack = 'Paper'
elif playerAttackKey == 'S' or playerAttackKey == 's':
playerAttack = 'Scissors'
if playerAttack == 'Rock' and enemyAttack == "rock":
printLine()
print (playerName + " attacks with ROCK!")
print (choiceMonster + " attacks " + playerName + " with ROCK!")
printLine()
print ("Tie! No one took or dealt any damage.")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Rock' and enemyAttack == "scissors":
printLine()
print (playerName + " attacks with ROCK!")
print (choiceMonster + " attacks " + playerName + " with SCISSORS!")
enemyHealth -= playerDamage
printLine()
print ("Nice hit! You did " + str(playerDamage) + " DMG to the " + choiceMonster + "!")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Rock' and enemyAttack == "paper":
printLine()
print (playerName + " attacks with ROCK!")
print (choiceMonster + " attacks " + playerName + " with PAPER!")
playerHealth -= enemyDamage
printLine()
print ("Ouch, you took some damage! The " + choiceMonster + " did " + str(enemyDamage) + " DMG to you!")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Paper' and enemyAttack == "paper":
printLine()
print (playerName + " attacks with PAPER!")
print (choiceMonster + " attacks " + playerName + " with PAPER!")
printLine()
print ("Tie! No one took or dealt any damage.")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Paper' and enemyAttack == "rock":
printLine()
print (playerName + " attacks with PAPER!")
print (choiceMonster + " attacks " + playerName + " with ROCK!")
enemyHealth -= playerDamage
printLine()
print ("Nice hit! You did " + str(playerDamage) + " DMG to the " + choiceMonster + "!")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Paper' and enemyAttack == "scissors":
printLine()
print (playerName + " attacks with PAPER!")
print (choiceMonster + " attacks " + playerName + " with SCISSORS!")
playerHealth -= enemyDamage
printLine()
print ("Ouch, you took some damage! The " + choiceMonster + " did " + str(enemyDamage) + " DMG to you!")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Scissors' and enemyAttack == "scissors":
printLine()
print (playerName + " attacks with SCISSORS!")
print (choiceMonster + " counter attacks " + playerName + " with SCISSORS!")
printLine()
print ("Tie! No one took or dealt any damage.")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Scissors' and enemyAttack == "paper":
printLine()
print (playerName + " attacks with SCISSORS!")
print (choiceMonster + " attacks " + playerName + " with PAPER!")
enemyHealth -= playerDamage
printLine()
print ("Nice hit! You did " + str(playerDamage) + " DMG to the " + choiceMonster + "!")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
elif playerAttack == 'Scissors' and enemyAttack == "rock":
printLine()
print (playerName + " attacks with SCISSORS!")
print (choiceMonster + " attacks " + playerName + " with ROCK!")
playerHealth -= enemyDamage
printLine()
print ("Ouch, you took some damage! The " + choiceMonster + " did " + str(enemyDamage) + " DMG to you!")
print ("")
print (playerName + "'s health: " + str(playerHealth) + " HP!")
print (choiceMonster + "'s health: " + str(enemyHealth) + " HP!")
printLine()
if enemyHealth <= 0:
timesWon += 1
print("Great job " + playerName + " for taking down the " + choiceMonster + "!")
print("Go now! There's no time to waste.")
print("You go deeper into an alley, trying to find a way to escape.")
printLine()
如果定义变量,那么代码没有问题:
没有无限循环,程序退出时你已经赢了。你知道吗
只要“enemyHealth”保持高于0,它就可以保持在while循环中。你知道吗
脚本:
相关问题 更多 >
编程相关推荐