Mohammed Omer Mohammed Omer - 6 months ago 15
Python Question

What is wrong with this block of code?

print("Here are the rules of the game \n"
"\n"
"1. There are 5 rooms in total \n"
"2. 2 rooms will have monsters which kill you when you enter them \n"
"3. 2 rooms have aids which will tell you one of the room which contains monsters \n"
"4. 1 room will be the exit which helps you finish the game \n"
"\n"
"\n")

print(" How to play the game \n"
"\n"
"1. Enter any number from 1-5 \n"
"2. wait and see how your luck plays out :D \n")

game_start = int(input(" \n Enter the number of the room you'd like to enter : "))

if game_start == 1:
print("You have just been eaten by a monster \n"
" GAME OVER! ")

if game_start == 2:
print("You have found one of the aid room. \n"
"Be wary of room 1 for a monster resides there")
game_start = game_start+1
game_start = int(input(" \n Enter the number of the room you'd like to enter : "))

if game_start == 3:
print("You have found one of the aid room. \n"
"Be wary of room 4 for a monster resides there")
game_start = game_start + 1
game_start = int(input(" \n Enter the number of the room you'd like to enter : "))

if game_start == 4:
print("You have just been eaten by a monster \n"
" GAME OVER! ")

if game_start == 5:
print("Congratulations , you have successfully escaped the void")


There is no error when I run this program. Just that the program doesn't display what it is supposed to. I have a feeling this has something to do with the fact that I'm adding 1 to game_start every time the user enters 2 or 3 but I'm not exactly sure what can be done to remedy this problem. Here is execution part after I execute the code :

When the user enters the number 5 :

Enter the number of the room you'd like to enter : 5
Congratulations , you have successfully escaped the void

Process finished with exit code 0


When the user enters the number 1 :

Enter the number of the room you'd like to enter : 1
You have just been eaten by a monster
GAME OVER!

Process finished with exit code 0


When the user enters the number 4 :

Enter the number of the room you'd like to enter : 4
You have just been eaten by a monster
GAME OVER!

Process finished with exit code 0


Code works absolutely fine to this point.

When the user enters the number 2 and than enters 1 :

Enter the number of the room you'd like to enter : 2
You have found one of the aid room.
Be wary of room 1 for a monster resides there

Enter the number of the room you'd like to enter : 1

Process finished with exit code 0


So why doesn't the program display the usual message that is required to be displayed when the user enters the number 1 but instead exits the program without displaying any message or error prompt.

This is my first time posting on this forum and I am an extreme beginner in programming so I would be grateful if none of you would chew me out.

Answer

When the user enters the number 2 and then enters 1 control has passed the point in your program that tests for a 1 input, you only havethe tests for 3, 4, and 5. Of course none of those will succeed so the program simply ends.

What you need to do is put your tests inside a loop. This allows you to ask for user input multiple times with a single input call, and it allows all your tests to be performed each time the user submits input.

The code below uses a while True loop. This loop will loop forever unless a break command is encountered. So when the player is eaten by the monster or successfully escapes the void we use the break statement to break out of the loop.

print("""Here are the rules of the game

  1. There are 5 rooms in total
  2. 2 rooms will have monsters which kill you when you enter them
  3. 2 rooms have aids which will tell you one of the room which contains monsters
  4. 1 room will be the exit which helps you finish the game
""")

print("""How to play the game
  1. Enter any number from 1-5
  2. wait and see how your luck plays out :D
""")

while True: 
    game_state = int(input("\nEnter the number of the room you'd like to enter: "))

    if game_state == 1:
        print("You have just been eaten by a monster")
        break

    elif game_state == 2:
        print("You have found one of the aid rooms. \n"
            "Be wary of room 1 for a monster resides there")

    elif game_state == 3:
        print("You have found one of the aid rooms. \n"
            "Be wary of room 4 for a monster resides there")

    elif game_state == 4:
        print("You have just been eaten by a monster")
        break

    elif game_state == 5:
        print("Congratulations , you have successfully escaped the void")
        break

print("                GAME OVER!             ")
Comments