Python Forum

Full Version: Format for elif
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi - I am new to Python and was attempting to code based on the guidance at https://www.youtube.com/watch?v=8uJFN7OZ...vF&index=4. I am running into a code format issue with elif (line 35). Can you please help me identify the format issue

import random

def msg(room):
  if room[msg]==' ':
    return "you have entered the " + room['name']+' '
  else:
    return room['msg']

def main():
  dirs = (0,0,0,0)
  entrance = {'name':'Entrance way','directions':dirs,'msg':''}
  livingroom = {'name':'Livingroom','directions':dirs,'msg':''}
  hallway = {'name':'Hallway','directions':dirs,'msg':''}
  diningroom = {'name':'Diningroom','directions':dirs,'msg':''}
  kitchen = {'name':'Kitchen','directions':dirs,'msg':''}
  familyroom = {'name':'Familyroom','directions':dirs,'msg':''}

  entrance['directions'] = (kitchen, livingroom, 0, 0)
  livingroom['directions'] = (diningroom, 0, 0, entrance)
  hallway['directions'] = (0, kitchen, 0, familyroom)
  kitchen['directions'] = (0, diningroom, entrance, hallway)
  diningroom['directions'] = (0, 0, livingroom, kitchen)
  familyroom['directions'] = (0,hallway,0,0)

  rooms = [livingroom,hallway,diningroom,kitchen,familyroom]
  room_with_eggs = random.choice(rooms)
  eggs_delivered = False
  room = entrance 
  print (' can you find the basket')

  while True:
    if eggs_delivered == True and room[name] == 'Entrance way':
      print('you have the basket')
      break;
    elif not eggs_delivered and room['name'] == room_with_eggs['name']:
      eggs_delivered = True   
      print ('There\'s is the basket and Johnny is sleeping' + 'right next to it and! you have delivered the eggs' + 'get out quick')
      room['msg']= ('you are back in the room' + room['name'] + '! you already delivered the eggs' + 'get out of here before Johnny wakes up'  )
    else:
      print(msg(room))
      room['msg'] = 'You are back in the '+ room['name']

      stuck = True
      
main()
Line 34 was not indented properly
Thank you. That worked.
Can you please help understand why the error message misleading ? If the error was correct, i could have tried that
Quote:Can you please help understand why the error message misleading ?
You first need to post the complete unaltered error traceback presented in BBCode error tags.
Can't comment on what we can't see.
Line 32 has an error, the variable name
    if eggs_delivered == True and room[name] == 'Entrance way':
instead of string 'name'
    if eggs_delivered == True and room['name'] == 'Entrance way':
Also in the function below
def msg(room):
  print(room)
  if room[msg]==' ':
    return "you have entered the " + room['name']+' '
  else:
    return room['msg']
if room[msg]==' ': is trying to use the function itself as a key i think you meant to have if room['msg']==' ':