EX43,I have a trouble

Hello friends:
I want to make a function version of ex43 but it will run twice and I can not find why,could someone help me ?
here are the codes:

def engine():
    starting_scene = 'doorway'
    scene_name = starting_scene
    while True:
        scene_name = map(scene_name)
        if scene_name == 'get_out':
            break

def map(scene_name):
    SCENE = {'doorway':doorway(),
    'living_room':living_room(),
    'get_out':'get_out'
    }
    scene = SCENE[scene_name]
    return scene


def doorway():
    print('hello this is my doorway,and next I\'ll show you living_room')
    input('>')
    return 'living_room'

def living_room():
    print('and this is my living_room and now you could leave ,bye!')
    input('>')
    return 'get_out'

engine()

Hello @SiriusA.

I guess your code does not look like that.
Put a

[code]

above where your code start.
And a

[/code]

below.
.
I have no idea whatsoever if this exercise can be done with functions instead of classes.
If so I guess it will be longer

In my editor ,I have put indentation in front of code below,maybe I should put up pictures.And I have learned zed said that there are three ways : class ,function and module,so I want try other
two way to make a similar script

but it will run twice ,and I just can not find out why:sweat_smile:

It did this to me as well. I can’t remember what I did to fix it (or if I even did). If I had to guess, it’s likely something to do with the game engine. You end up using this module at the end of the book, and I haven’t encountered this issue after refactoring the code.

You could always try to debug it by using print statements and seeing if that uncovers anything.

ok,I 'll have a try thank you

Alright @SiriusA, you need to put print() at key points in your code. Whenever you say to yourself “i don’t know why” you have to take the time to debug it and find out more information, and look in places you have not looked yet. What you do is this:

  1. At the top and bottom of every function do this:
def afunction():
    print(">>>>> afunction")
    # your code is here
    print("<<<<< afunction")
  1. This will trace where your code is going. You should also add any parameters to functions to this print like this:
print(">>>>> afunction, thing=", thing)
  1. Run your code, and keep adding print lines, printing out variables, until you see why it’s running more than once.

Try those things, then post back your code with the prints so I can confirm you did it right. Then I’ll give you more advice. Remember to put [code] [/code] around your code. I edited your post so you can see what to do.

@zedshaw Thank your advice,after using your trick,I’ve found out the reason that is the dict calls the functions.After trying a lot times ,I remove the () parentheses in the value of dictionary and add it to scene=SCENE[scene_name]()<== in map function . And the script can run as I expecting.

def engine():
    print('>>>>>>> engine')
    starting_scene = 'doorway'
    scene_name = starting_scene
    while True:
        scene_name = map(scene_name)
        if scene_name == 'get_out':
            break
    print('<<<<<<< engine')

def map(scene_name):
    print('>>>>>>>  map')
    print('>>>>>>>  map_scene_name is:' + scene_name)

    SCENE = {
        'doorway':doorway,
        'living_room':living_room,
        'get_out':'get_out'
        }
    scene = SCENE[scene_name]()
    print('>>>>>>> map_secen ='+ scene)

    print('<<<<<<< map')
    return scene


def doorway():
    print('>>>>>>>>>> doorway')

    print('hello this is my doorway,and next I\'ll show you living_room')
    input('>')

    print('<<<<<<<<<< doorway')
    return 'living_room'


def living_room():
    print('>>>>>>>>>> living_room')

    print('and this is my living_room and now you could leave ,bye!')
    input('>')

    print('<<<<<<<<<< living_room')
    return 'get_out'

engine()


Is this regular or right?

Hmm, I’d say that’s one way to do it, but what I’d do is get rid of this line:

scene = SCENE[scene_name]()

Change that line to:

scene = SCENE[scene_name]

Then, that is returned to engine when you call map(scene_name) in engine(). Right after that line, call this scene:

scene = map(scene_name)
scene_name = scene()

Also, it seems like you might be hacking at this until it works rather than understanding it. Do you know why putting () where you did worked? Do you know why mine would work too?

I think when I put () after a function name ,I call it.And if you put a function with () in a dictionary as a value ,it will be called automatically.so I have to remove (),and it seems like cite an address
rather than call it .
The different between us is you call the function in the while circle of engine(), and I call the function in map().
And in this line <scene_name = map(scene_name)>,it will call tow functions,one is map(),another is in map().This make the code not so clear and complex.and yours is better than me ,and it is easy to understand.I will learn to type codes in a clear way.

Yeah, that’s close. Another way to think about it is, when you write the name of a function, that just retrieves it from memory like any other variable. So you can pass it around, assign it to things, put it in a dict, etc.

It’s when you put () after a name that Python then realizes it’s a function and tries to call it. Make sense?