无法继续我的代码(无限循环?)

2024-06-23 19:16:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码陷入了一个无限循环,我需要帮助修复它。 每当玩家(我)杀死怪物(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()

Tags: towithhpprinthealthelifstrattacks
2条回答

如果定义变量,那么代码没有问题:

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()

没有无限循环,程序退出时你已经赢了。你知道吗

只要“enemyHealth”保持高于0,它就可以保持在while循环中。你知道吗

脚本:

enemyHealth =100

while enemyHealth > 0:

... snippet Your code....

    if .... :
        enemyHealth = -1

... snippet Your code....

... other code ....

相关问题 更多 >

    热门问题