我目前正在研究Zed Shaw的学习Python的艰难方法。我正在努力练习练习43,它指示我创建一个具有以下属性的文本游戏:
到目前为止,我已经启动了两个文件,一个用于跑步者,一个用于房间:
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
我的问题是我无法让game_runner进入下一个房间。当我运行它时,它会播放第一个房间的无限循环:打印Chillin()的文档字符串,请求输入,然后重复。
在第一堂课中输入正确答案后,如何更改我的跑步者和/或地图以返回下一堂课,即Pregame()?
答案 0 :(得分:6)
我认为你需要做的就是(如果我正确地遵循你的代码)改变了这个:
next_room.proceed()
到此:
next_room = next_room.proceed()
您永远不会重新分配您在while True:
循环中使用的变量,因此您将永远获得相同的行为。