Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format for elif
#1
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()
Reply
#2
Line 34 was not indented properly
Reply
#3
Thank you. That worked.
Can you please help understand why the error message misleading ? If the error was correct, i could have tried that
Reply
#4
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.
Reply
#5
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']==' ':
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,214 Jul-28-2019, 05:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020