Reno
Reno

Reputation: 1139

What is wrong with my simple code here?

Hi I am having problems writing this simple program. I am just starting out with Python and was hoping for some help. When I run the start() function at the bottom of the programme, everything works fine until after the first raw_input(). If the user types "get coffee" for example, the string "Fair enough take a break" is printed but after this, rather than running the coffee() function like I would like, it just loops through to the start() function again.

Would someone be able to help please? Thanks a lot.

def engine(next_scene):
    scenes = {"start":start(),"coffee":coffee(),"work":work()}
    return scenes[next_scene]

def start():
    print "you are in the office"
    print "you wonder what to do"
    action = raw_input("what do you do? Get coffee or work?")

    if action == "get coffee":
        print "Fair enough take a break"
        next_scene = "coffee"
        engine(next_scene)
    if action == "work":
        print "Good man, you are well on your way to being a coder"
        next_scene = "work"
        engine(next_scene)

def coffee():
    print "You walk out of the room"
    print "You head down the stairs and into the cafe"
    print "You order an espresso"
    print "You neck it down"
    print "Yumm"
    print "You are wired"
    action = raw_input("Now what? Work or go home? > ")

    if action == "work":
        print "You head back upstairs"
        next_scene = "work"
        engine(next_scene)
    if action == "go home":
        print "You lazy git"

def work():
    print "You beaver away and become a cool coder"
    next_scene = "start"
    engine(next_scene)

start()

Upvotes: 1

Views: 99

Answers (2)

Anurag
Anurag

Reputation: 1013

Your engine function should like.

def engine(next_scene):
    scenes = {"start":start,"coffee":coffee,"work":work}
    scenes[next_scene]()

Upvotes: 1

Matthias
Matthias

Reputation: 13222

This

scenes = {"start":start(),"coffee":coffee(),"work":work()}

should be

scenes = {"start":start,"coffee":coffee,"work":work}

You called the functions in the dictionary definition, but you just want to get the function object.

Upvotes: 4

Related Questions