<p>如果定义变量,那么代码没有问题:</p>
<pre><code>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()
</code></pre>
<p>没有无限循环,程序退出时你已经赢了。你知道吗</p>