0

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

3 Answers3

0

It works just take out the while loop from inside

    def main(): ...

while True:
    main()
    retry = input("Do you want to try again?") # When the code is     
    if retry == "no": 
        break
Cristian
  • 17
  • 1
0

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.

  • Probably not an issue, but this will not allow more than 1000 games to be played in succession due to default recursion limits. – JonSG Jul 13 '23 at 17:06
0

The question is a little bit confusing, but I think you want to fix the fact that:

1) The treasure box only prints if you enter the last else statement,
2) the while loop only executes if the last else statement is true, and
3) you cannot get out of the while loop if the user says 'yes'.
(Scroll to the bottom if you just want to see the solution.)

To fix Problem 1, just call main() right after the function is called, and delete the main() function call inside the while loop (unless you want it there.)

To fix Problem 2, add a variable game_over and set it to False at the beginning of the code, after import sys (I don't see where you use that, so check the usages of the import statement.) Then, to the end of:

else:
    print("\nYou get trapped! Game over!")

And:

else:
    print("You were attacked by evil robots! Game Over!")

You can add:

game_over = True

You can do the same for:

else:
    print("You fell into a hole! Game Over!")

But, delete the while loop. Instead, move it out of everything, and put the while loop inside a conditional:

if game_over:  # This code is not final. It will still be changed later.
    while True:
        main()  # If you want.
        retry = input("Do you want to try again?")
        if retry == "no":
            break

To fix Problem 3, simply move the while loop to the outermost indentation (i.e. no indent). This way, any input that is not 'no' will be interpreted as 'yes' and start the game again.

# This is the final code:
import sys


def main():  # Sorry for bad indentation, couldn't fix it somehow

    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_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/_____ /
*******************************************************************************
''')
game_over = False
while True:
    main()
    print("Welcome to the Programming Universe!.")
    print("Your mission is to get all the 10 laptops for programming.\n") 
    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!")
                game_over = True
        else:
            print("You were attacked by evil robots! Game Over!")
            game_over = True
    else:
        print("You fall into a hole! Game Over!")
        game_over = True
    if game_over:
        main()  # If you need it.
        retry = input("Do you want to try again?")
        if retry == "no":
            break