尝试在Zed Shaw的LPTHW中为ex 45制作我自己的RPG角色生成器。作业的一部分是为程序的每个"房间"创建一个新类,如WelcomeScreen
或ChooseMutations
。
这是主要程序。
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 'ttWelcome to the'
print 'ttMetamorphosis Alpha Character & Random Encounter Generator'
print 'ttProgrammed 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 中运行主程序时,它只是注销而不是从 rooms
返回函数get_name()
。输出发布在下面。
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$
如果我的问题标题不完全是我想问的,我提前道歉,作为一个新手,有时很难不知道到底该问什么。
您返回的是一个字符串,而不是一个函数(或函数结果)。 您可能想要类似以下内容:
def hello_user(self):
return self.get_name
或
def hello_user(self):
return self.get_name()
根据您的程序,我认为您可能想要第二个。 不同之处在于,第一个返回get_name
函数,而第二个返回get_name
函数的结果。