Home > Back-end >  Python else statement not doing anything
Python else statement not doing anything

Time:06-06

In my code, I wrote an if statement responding to input, followed by an else statement, which repeats the function defining the input. If I respond to the input with what's not in the if statement, the program terminates right there, and I can't seem to figure out why.

whatdo = False
room = 0
inventory = [0, 0, 0, 0, 0]
def dowhatdo():
    if whatdo == True:
        global quest
        quest = input("What do you do?: ")
        

if room == 1:
    whatdo = True
    print('You wake up in a room with walls made of stone. A large 
door stares at you. To the side of the room is a chest.')
    dowhatdo()
    if quest == "open chest":
        print("You found a key. You pick it up.")
        inventory[0] = "room1key"
        dowhatdo()
    else:
        print('Huh?')
        dowhatdo()

CodePudding user response:

You want the program to keep running, right? Therefore you need a loop (and an exit condition)

whatdo = False
room = 1 # change this to enter the program
def dowhatdo():
    if whatdo == True:
        global quest
        quest = input("What do you do?: ")

# Infinite loop  
while 1:
  if room == 1:
      whatdo = True
      print('You wake up in a room with walls made of stone. A large door stares at you. To the side of the room is a chest.')
      dowhatdo()
      # exit condition
      if quest == "exit":
        break
      if quest == "open chest":
          print("You found a key. You pick it up.")
          # inventory[0] = "room1key"
          dowhatdo()
      else:
          print('Huh?')
          dowhatdo()
  

CodePudding user response:


whatdo = False
room = 1 # to enter the while loop
def dowhatdo():
    if whatdo == True:
        return input("What do you do?: ")
        

if room == 1:
    whatdo = True
    print('You wake up in a room with walls made of stone. A large 
door stares at you. To the side of the room is a chest.')
   
    if  dowhatdo() == "open chest":
        print("You found a key. You pick it up.")
        inventory[0] = "room1key" # ???
        dowhatdo()
    else:
        print('Huh?')
        dowhatdo()
  • Related