Search code examples
pythonif-statementwhile-loop

How do I repeat a code and use while loop in an if/else statement?


I want to repeat a whole code in an else statement using while loops. I already tried the following, it does not work and when the code is false it does not print anything in the else statement and while loop!. How do I fix this?

# Start()
import sys
def main():

    print('''
    *******************************************************************************
            |                   |                  |                     |
    _________|________________.=""_;=.______________|_____________________|_______
    |                   |  ,-"_,=""     `"=.|                  |
    |___________________|__"=._o`"-._        `"=.______________|___________________
            |                `"=._o`"=._      _`"=._                     |
    _________|_____________________:=._o "=._."_.-="'"=.__________________|_______
    |                   |    __.--" , ; `"=._o." ,-"""-._ ".   |
    |___________________|_._"  ,. .` ` `` ,  `"-._"-._   ". '__|___________________
            |           |o`"=._` , "` `; .". ,  "-._"-._; ;              |
    _________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______
    |                   | |o;    `"-.o`"=._``  '` " ,__.--o;   |
    |___________________|_| ;     (#) `-.o `"=.`_.--"_o.-; ;___|___________________
    ____/______/______/___|o;._    "      `".o|o_.--"    ;o;____/______/______/____
    /______/______/______/_"=._o--._        ; | ;        ; ;/______/______/______/_
    ____/______/______/______/__"=._o--._   ;o|o;     _._;o;____/______/______/____
    /______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
    ____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
    /______/______/______/______/______/______/______/______/______/______/_____ /
    *******************************************************************************
    ''')
    print("Welcome to the Programming Universe!.")
    print("Your mission is to get all the 10 laptops for programming.\n") 

    #Write your code below this line 👇
    direction = input("There are two directions right or left?: ").lower()
    if direction == "right":
        swim_or_wait = input("Do you want to wait or swim?: ").lower()
        if swim_or_wait == "wait":
            option = input("\nWhich language is the easiest? [1]Python, [2]C++, [3]C#, [4]Lua, [5]JS: ").lower()
            if option == "python" or option == "1":
                print("You got all the 10 laptops for programming! You Win")
            else:
                print("\nYou get trapped! Game over!")
        else:
            print("You were attacked by evil robots! Game Over!")
    else: # Here I want the while loop in this else statement like the code in it.
        print("You fall into a hole! Game Over!")
        while True:
            main()
            retry = input("Do you want to try again?") # When the code is false it does not print anything in this else statement and while loop!
            if retry == "no": # How do I fix this code?
                break


I tried using while loop like this. Also this is the part of the code that is not executing due to the while loop.

 else: # Here I want the while loop in this else statement like the code in it.
        print("You fall into a hole! Game Over!")
        while True:
            main()
            retry = input("Do you want to try again?") # When the code is false it does not print anything in this else statement and while loop!
            if retry == "no": # How do I fix this code?
                break

Solution

  • I don't understand why you would use a while loop for this. What I think you want to do is printing Game Over, then asking "Do you want to try again?" and if the answer is anything but "no", repeat the game by calling the main() function. You don't need a while loop for this:

        else:
            print("You fall into a hole! Game Over!")
            retry = input("Do you want to try again?")
            if retry == "no":
                exit()  # Exit the Game
            main()  # Reset the Game
    

    By using this recursive structure, your game can run endlessly without a while loop.