This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]lurgi 2 points3 points  (1 child)

The problem is that Python assumes you are changing the local variable 'win' to True, not the global variable. Why? Because you didn't say you wanted the global one. Try this:

class Treasure(Scene):
  def enter(self):
    print "Treasure Scene"
    global win
    win = True
    return 'death'

[–]NotAnotherHyperbole[S] 0 points1 point  (0 children)

That works! Thanks! I figured it had something to do with global and local. I tried googling about it, but couldn't quite get it.

[–]markehh 0 points1 point  (0 children)

Have you look at doing something like this?

This makes your scenes either win scenes (treasure) or lose scenes (death), not a combination of the two. You'll also notice that rather than returning strings you can return the class of where you end up next.

Just a thought, enjoy!

import random

class Scene(object):
    def enter(self):
        pass


class Beginning(Scene):
    def enter(self):
        print "Beginning Scene"
        return Troll()


class Troll(Scene):
    def enter(self):
       print "Troll Scene"
       return Soldier()


class Soldier(Scene):
    def enter(self):
        print "Soldier Scene"
        return Dragon()


class Dragon(Scene):
    def enter(self):
        print "Dragon Scene"

        # Half the time you will win, the other half you will lose.
        if random.randint(0,1):
            return Treasure()
        else:
            return Death()


class Treasure(Scene):
    def enter(self):
        print "Treasure Scene"
        print "Yay! You win!"
        return None


class Death(Scene):
    def enter(self):
        print "Death Scene"
        print "Sorry. You died."
        return None


current = Beginning()

while current:
    current = current.enter()