如何使此程序继续?就这么停了

2024-05-08 22:05:13 发布

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

import random
import time


print("Welcome to the Dojo!")
print("You have three opponents; they are ready...")
print("Are you?")
print("*To view the rules, type 'rules' ")
print("*To view commands, type 'commands' ")
print("*To begin, type 'start' ")

while True:
    userInput = input()
    # Rules
    if userInput == "rules":
        print("The rules in this Dojo are simple. Kill your opponent! Fight to the death! Show no mercy!")
        print("Each opponent gets progressively more difficult, whether it be in terms of health or damage.")
        print("To attack, type 'attack'")
        print("May the better (luckier) warrior win")
    # Commands - to be added

        # print("Commands will be added soon!")



    # Start
    elif userInput == "start":


        '''
        print("Your first opponent is Larry Schmidt. Don't sweat it, he'll be a piece of cake.")
        time.sleep(3)
        print("The battle will begin in")
        time.sleep(1)
        print("5")
        time.sleep(1)
        print("4")
        time.sleep(1)
        print("3")
        time.sleep(1)
        print("2")
        time.sleep(1)
        print("1")
        time.sleep(1)
        print("Fight!")
        '''

        userHealth = 100
        opponentHealth = 100
        while opponentHealth > 0 or userHealth > 0:

            userInput = input()

            if userInput == "attack":
                userDamage = random.randint(0, 100)
                opponentDamage = random.randint(0, 20)
                opponentOne = "Larry"
                opponentHealth -= userDamage

                if opponentHealth <= 0:
                    opponentHealth = 0

                if (userDamage < 25) and (userDamage > 0):
                    quoteOptions = [("You roundhouse kick {} in the abdomen and he begins to vomit.".format(
                        opponentOne)), ("You punch {} in the stomach and he begins to tear up.".format(opponentOne))]

                    print(random.choice(quoteOptions))
                    time.sleep(1)
                    print("You did {} damage to him".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")


                elif (userDamage < 100) and (userDamage > 25):
                    quoteOptions = [("You drive your foot into {}'s groin with as much force as possible. You hear a high-pitched scream emit from his vocal chords.".format(
                            opponentOne)), ("{} is intimidated by you and decides to slap himself mindlessly, in hopes that he will lose faster.".format(
                            opponentOne))]
                    print(random.choice(quoteOptions))
                    time.sleep(1)
                    print("You did {} damage to him; a CRITICAL HIT!".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")


                elif userDamage == 100:
                    print("{} forfeits... Coward.".format(opponentOne))
                    time.sleep(1)
                    print("You did {} damage to him. INSTANT K.O".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")

                else:
                    quoteOptions = [("Swing and a miss. You missed {}.".format(opponentOne)), ("You underestimated {}".format(
                        opponentOne))]
                    print(random.choice(quoteOptions))
                    time.sleep(1)
                    print("You did {} damage to him.".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")


                # Opponent Attack
                if opponentHealth - 1 < opponentHealth:
                    userHealth = userHealth - opponentDamage
                    time.sleep(2)
                    print("Larry gathers the courage to punch you. Meh." or "Larry throws his shoe at you and starts to yell frantically while running away from you... You catch up soon enough, without breaking much of a sweat.")
                    time.sleep(1)
                    print("He did {} damage to you." .format(opponentDamage))
                    time.sleep(1)
                    print("You have {} health remaining." .format(userHealth))
                    print("---------------------------------------------------")

                if userHealth <= 0:
                        print("You lose!")
                        print("Come back do the Dojo when you become stronger. You are welcome back anytime, grasshopper.")
                        quit()

                elif opponentHealth <= 0:
                        print("You win!")
                        break




        #Next Opponent

        userHealth = 100
        opponentHealth = 100
        while opponentHealth > 0 or userHealth > 0:
            if opponentHealth == 0:

                print("Nice work, but don't get cocky now. You've got two more opponents ahead of you.")
                time.sleep(1)
                print("Your next opponent is Goliath the Greatest. He really knows how to put up a fight. Don't let him beat you!")
                time.sleep(1)
                print("The battle will begin in")
                time.sleep(1)
                print("5")
                time.sleep(1)
                print("4")
                time.sleep(1)
                print("3")
                time.sleep(1)
                print("2")
                time.sleep(1)
                print("1")
                time.sleep(1)
                print("Fight!")

            userInput = input()

            if userInput == "attack":
                userDamage = random.randint(0, 75)
                opponentDamage = random.randint(0, 50)
                opponentTwo = "Goliath the Greatest"
                opponentHealth -= userDamage

                if opponentHealth <= 0:
                    opponentHealth = 0

                if (userDamage < 25) and (userDamage > 0):
                        quoteOptions = [("You roundhouse kick {} in the abdomen and he begins to vomit.".format(
                            opponentTwo)), ("You punch {} in the stomach and he begins to tear up.".format(opponentTwo))]

                        print(random.choice(quoteOptions))
                        time.sleep(1)
                        print("You did {} damage to him".format(userDamage))
                        time.sleep(1)
                        print("He has {} health remaining.".format(opponentHealth))
                        print("---------------------------------------------------")


                elif (userDamage < 100) and (userDamage > 25):
                    quoteOptions = [("You drive your foot into {}'s groin with as much force as possible. You hear a high-pitched scream emit from his vocal chords.".format(
                            opponentTwo)), ("{} is intimidated by you and decides to slap himself mindlessly, in hopes that he will lose faster.".format(
                            opponentTwo))]
                    print(random.choice(quoteOptions))
                    time.sleep(1)
                    print("You did {} damage to him; a CRITICAL HIT!".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")


                elif userDamage == 100:
                    print("{} forfeits... Coward.".format(opponentTwo))
                    time.sleep(1)
                    print("You did {} damage to him. INSTANT K.O".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")

                else:
                    quoteOptions = [("Swing and a miss. You missed {}.".format(opponentTwo)), ("You underestimated {}".format(
                        opponentTwo))]
                    print(random.choice(quoteOptions))
                    time.sleep(1)
                    print("You did {} damage to him.".format(userDamage))
                    time.sleep(1)
                    print("He has {} health remaining.".format(opponentHealth))
                    print("---------------------------------------------------")


                # Opponent Attack
                if opponentHealth - 1 < opponentHealth:
                    userHealth = userHealth - opponentDamage
                    time.sleep(2)
                    print("Larry gathers the courage to punch you. Meh." or "Larry throws his shoe at you and starts to yell frantically while running away from you... You catch up soon enough, without breaking much of a sweat.")
                    time.sleep(1)
                    print("He did {} damage to you." .format(opponentDamage))
                    time.sleep(1)
                    print("You have {} health remaining." .format(userHealth))
                    print("---------------------------------------------------")

                if userHealth == 0:
                    print("You lose!")
                    print("Come back do the Dojo when you become stronger. You are welcome back anytime, grasshopper.")
                    quit()
                elif opponentHealth == 0:
                    print("You win!")
                    break





            else:
                print("Type 'attack' to attack.")





# Invalid response
    else:
        print("Enter a valid response, young grasshopper.")
        if userInput == "start" is True:
            continue

我知道这是可怕的代码,但这是我的第一个。每件事至少都能成功,直到它说“你赢了”。它应该继续到下一场战斗,但它只是停留在那里。我的主要问题在哪里(除了糟糕的格式和多余的代码)?你知道吗


Tags: andthetoyouformatiftimesleep