Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
艰苦地学习Python,练习43_Python - Fatal编程技术网

艰苦地学习Python,练习43

艰苦地学习Python,练习43,python,Python,我目前正致力于Zed Shaw的《艰苦学习Python》。我正在努力练习43,它指示我创建一个具有以下属性的文本游戏: 使用多个文件 每个“房间”一节课 到目前为止,我已经创建了两个文件,一个用于跑步者,另一个用于房间: 游戏\u runner.py from game_map import * class Runner(object): def __init__(self, start): self.start = start def play(self

我目前正致力于Zed Shaw的《艰苦学习Python》。我正在努力练习43,它指示我创建一个具有以下属性的文本游戏:

  • 使用多个文件
  • 每个“房间”一节课
到目前为止,我已经创建了两个文件,一个用于跑步者,另一个用于房间:

游戏\u 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()
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
游戏地图.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()
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
我的问题是,我无法让比赛选手进入下一个房间。当我运行它时,它会播放第一个房间的无限循环:打印Chillin()的docstring,请求输入,然后重复


在第一个类中输入正确的响应后,如何更改runner和/或map以返回下一个类,即Pregame()

我认为您所需要做的(如果我正确地遵循您的代码)就是更改以下内容:

next_room.proceed()
为此:

next_room = next_room.proceed()

你永远不会重新分配你在
循环中使用的变量,而True:
循环,所以你永远都会得到相同的行为。

伙计,这个代码看起来像一个带有潜意识信息的啤酒广告