Steve L
Steve L

Reputation: 1714

Learn Python the Hard Way, Exercise 43

I am currently working on Learn Python the Hard Way by Zed Shaw. I am struggling with Exercise 43, which instructs me to create a text game with the following properties:

So far I have started two files, one for the runner and one with the rooms:

game_runner.py

from game_map import *

class Runner(object):
    def __init__(self, start):
        self.start = start

    def play(self):
        next_room = self.start

        while True:
            print '\n'
            print '-' * 7
            print next_room.__doc__
            next_room.proceed()

firstroom = Chillin()

my_game = Runner(firstroom)

my_game.play()

game_map.py

from sys import exit

class Chillin(object):
    """It's 8pm on a Friday night in Madison. You're lounging on the couch with your 
roommates watching Dazed and Confused. What is your first drink?
    1. beer
    2. whiskey
    3. vodka
    4. bowl
"""
    def __init__(self):
        self.prompt = '> '

    def proceed(self):
        drink = raw_input(self.prompt)

        if drink == '1' or drink == 'beer':
            print '\n Anytime is the right time.'
            print 'You crack open the first beer and sip it down.'
            room = Pregame()
            return room
        #rest of drinks will be written the same way


class Pregame(object):
    """It's time to really step up your pregame.
How many drinks do you take?
"""

    def proceed(self):
        drinks = raw_input('> ')
    #and so on

My problem is that I cannot get the game_runner to proceed to the next room. When I run it, it plays an infinite loop of the first room: prints the docstring for Chillin(), asks for input, then repeats.

How can I change my runner and/or map to return the next class, i.e. Pregame(), after entering a correct response in the first class?

Upvotes: 3

Views: 1690

Answers (1)

g.d.d.c
g.d.d.c

Reputation: 47988

I think all you need to do (if I follow your code correctly) is change this:

next_room.proceed()

to this:

next_room = next_room.proceed()

You're never re-assigning to the variable that you're using inside your while True: loop, so you get the same behavior forever.

Upvotes: 6

Related Questions