我的代码陷入了无限循环,我需要修复它的帮助。 每当玩家(我)杀死怪物(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()
答案 0 :(得分:0)
只要'enemyHealth'保持大于0,就表示它停留在while循环中。
脚本:
enemyHealth =100
while enemyHealth > 0:
... snippet Your code....
if .... :
enemyHealth = -1
... snippet Your code....
... other code ....
答案 1 :(得分:0)
如果定义变量,代码没有错:
import random
enemyHealth = 2
def printLine():
print('-----------------')
playerName = 'sup'
choiceMonster = 'bro'
playerDamage = 2
playerHealth = 999
enemyDamage = 0
timesWon = 0
while enemyHealth > 0:
enemyAttack = random.choice (['rock', 'paper', 'scissors'])
playerAttackKey = input(f"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 {playerDamage} 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()
没有无限循环,您赢了,程序就会退出。