简体   繁体   中英

How to make the program go back to the top of the code with a while loop?

I need to include a decrement life counter that has 5 lives. I need to use a while loop and once the player loses a life it needs to send them back to the choice between going left and right in the code. I am new to python so I am not very familiar with it, any help is appreciated.

answer = input("Do you want to go on an adventure? (Yes/No) ")
if answer.lower().strip() == "yes":
   
    x=5
    while x > 0:
        print("You have ",x,"lives left.") 
        if x > 0:
            break
        x-=1
        if x == 0:
                break
    answer= input("You are lost in the forest and the path splits. Do you go left or right? (Left/Right) ").lower().strip()
    if answer == "left":
 
        answer = input("An evil witch tries to cast a spell on you, do you run or attack? (Run/Attack) ").lower().strip()
        if answer == "attack":
            print("She turned you into a green one-legged chicken, you lost!")
 
        elif answer == "run":
            print("Wise choice, you made it away safely.")
            answer = input("You see a car and a plane.  Which would you like to take? (Car/Plane) ").lower().strip()
            if answer == "plane":
                print("Unfortunately, there is no pilot. You are stuck!")
            elif answer == "car":
                print("You found your way home. Congrats, you won!")
 
            elif answer != "plane" or answer != "car":
                print("You spent too much time deciding...")
        else:
            print("You are frozen and can't talk for 100 years...")
    elif answer == "right":
     
        import random 
        num = random.randint(1, 3)  
        answer = input("Pick a number from 1 to 3: ")
        if answer == str(num):
            print("I'm also thinking about {} ".format(num))
            print("You woke up from this dream.")
        elif answer != num: 
            print("You fall into deep sand and get swallowed up. You lost!")
    else:
        print("You can't run away...")
 
else: 
    print("That's too bad!")

  1. You should try to add comments to your code, it will help you and others as well.
  2. I think for getting user answer, you should use a different variable. It will make things easier.

I removed the following code, it didn't make any sense to me.

while x > 0:
        print("You have ",x,"lives left.") 
        if x > 0:
            break
        x-=1
        if x == 0:
                break
    

--

This is the code, which works:

 # Try to write imported modules at the top, it's a good practice.
import random 

# Start the game
answer_start = input("Do you want to go on an adventure? (Yes/No) ")

# user chooses to play
if answer_start.lower().strip() == "yes":   
    lives=5
    # function for displaying the lives    
    def display_lives():
        print("You have ",lives,"lives") 

    #display lives
    display_lives()

   # As long lives are more than 0, this will keep going. 
    while lives>0:
        #First choice
        answer1= input("You are lost in the forest and the path splits. Do you go left or right? (Left/Right) ").lower().strip()

        #User chooses left
        if answer1 == "left":
            answer2 = input("An evil witch tries to cast a spell on you, do you run or attack? (Run/Attack) ").lower().strip()
            if answer2 == "attack":
                print("She turned you into a green one-legged chicken, you lost!")
                lives=lives-1
                display_lives()

        # User is running away
            elif answer2 == "run":
                print("Wise choice, you made it away safely.")
                answer3 = input("You see a car and a plane.  Which would you like to take? (Car/Plane) ").lower().strip()
                if answer3 == "plane":
                    print("Unfortunately, there is no pilot. You are stuck!")
                elif answer3 == "car":
                    print("You found your way home. Congrats, you won!")

                elif answer3 != "plane" or answer3 != "car":
                    print("You spent too much time deciding...")
            else:
                print("You are frozen and can't talk for 100 years...")
        elif answer1 == "right":


            num = random.randint(1, 3)  
            answer4 = input("Pick a number from 1 to 3: ")
            if answer4 == str(num):
                print("I'm also thinking about {} ".format(num))
                print("You woke up from this dream.")
            elif answer4 != num: 
                print("You fall into deep sand and get swallowed up. You lost!")
                lives=lives-1
        else:
            print("You can't run away...")

    #Player chose not to play or player out of lives.
else: 
        print("That's too bad!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM