0

试图在 Zed Shaw 的 LPTHW 中为 ex 45 制作我自己的 RPG 角色生成器。部分任务是为程序的每个“房间”创建一个新课程,例如WelcomeScreenChooseMutations

这是主程序。

import rooms

class Program(object):

    def __init__(self, start):
        self.start = start

    def run(self):
        next_room_name = self.start

        while True:
            room = getattr(self, next_room_name)
            next_room_name = room()

x = rooms.WelcomeRoom()

Program(x.hello_user())

这是rooms它试图从中提取内容的文件。

class WelcomeRoom(object):

    def __init__(self):
        pass

    def hello_user(self):
        print '*' * 79
        print '\n'
        print '\t\tWelcome to the'
        print '\t\tMetamorphosis Alpha Character & Random Encounter Generator'
        print '\t\tProgrammed poorly by Raymond Weiss'
        print '\n'
        print '*' * 79
        raw_input('Please press enter to continue')
        return 'get_name'

    def get_name(self):

        name = raw_input('Hello, whats your name?', 
                 '\n',
                 ':> ')

但是当我在 python 中运行主程序时,它只是注销而不是get_name()rooms. 输出发布在下面。

Raymond-Weisss-MacBook-Pro:macgre Raylug$ python macgre.py
*******************************************************************************


        Welcome to the
        Metamorphosis Alpha Character & Random Encounter Generator
        Programmed poorly by Raymond Weiss


*******************************************************************************
Please press enter to continue
Raymond-Weisss-MacBook-Pro:macgre Raylug$ 

如果我的问题标题不是我想要问的,我提前道歉,作为一个新手,有时很难不知道到底要问什么。

4

1 回答 1

1

您返回的是字符串,而不是函数(或函数结果)。你可能想要这样的东西:

def hello_user(self):
    return self.get_name

或者

def hello_user(self):
    return self.get_name()

根据您的程序,我认为您可能想要第二个。区别在于第一个返回get_name 函数,而第二个返回函数的结果get_name

于 2012-12-14T14:05:05.223 回答