Python 尝试运行方法时,获取';非类型不可调用';
我是编程新手,所以请温柔一点。在过去几天左右的时间里,为了让代码正常工作,我在stackoverflow内外做了大量的研究(我想我也做了很多),所以希望我不会第十亿次重复一个问题。 另外,我为穷人道歉,我的代码可能很难读懂。我这样做是为了教自己如何使用类和方法 问题在rm1方法中的Python 尝试运行方法时,获取';非类型不可调用';,python,oop,class,methods,Python,Oop,Class,Methods,我是编程新手,所以请温柔一点。在过去几天左右的时间里,为了让代码正常工作,我在stackoverflow内外做了大量的研究(我想我也做了很多),所以希望我不会第十亿次重复一个问题。 另外,我为穷人道歉,我的代码可能很难读懂。我这样做是为了教自己如何使用类和方法 问题在rm1方法中的返回self.rm3和rm3本身的代码之间 我的代码是: from sys import exit from random import randint def death(): print 'You are
返回self.rm3
和rm3本身的代码之间
我的代码是:
from sys import exit
from random import randint
def death():
print 'You are "dead", I guess.'
exit(0)
def inp(prompt='What do you do? '):
to_return = raw_input(prompt).lower()
if to_return == 'q' or to_return == 'quit':
print "Bye now~"
exit(0)
else:
return to_return
class Rooms(object):
def __init__(self):
current_room = self.rm1
self.items = []
while True:
current_room = current_room()
def rm1(self):
print "=== You are in Room 1 ==="
print "You are in a dark room."
print "Obvious exits are east and south."
while True:
choice = inp()
if choice == "east":
return self.rm2
elif choice == "south":
return self.rm3
elif choice == "look":
return self.rm1
def rm2(self):
print "=== You are in Room 2 ==="
if "haircomb" not in self.items:
print "There is a high-tech looking keypad on the southernly wall,"
print "next to the door."
print "Upon closer inspection there are not numbers, but three"
print "letters on the keypad: A, M, and Z."
print "Do you try to guess the code?"
print "Obvious exits are west and south."
while True:
choice = inp()
if "guess code" in choice and "haircomb" not in self.items:
return self.rm2guess
elif choice == "west":
return self.rm1
elif choice == "south" and "haircomb" not in self.items:
print "The door is firmly locked."
elif choice == "south" and "haircomb" in self.items:
return self.rm4
elif choice == "look":
return self.rm2
def rm2guess(self):
correct_answer = randint(1,3)
guesses_left = 2
print "You approach the keypad."
while True:
guess = inp("Which key do you press? ").lower()
if guess == "a" or guess == "m" or guess == "z":
if guess == "a":
guess = 1
elif guess == "m":
guess = 2
elif guess == "z":
guess = 3
if guess == correct_answer:
print "The machine whirrs and then beeps."
print "The southernly door seems to be open now."
print "Suddenly, a haircomb falls from the bottom of the "
print "keypad. You take it."
self.items.append("haircomb")
return self.rm2
elif guess != correct_answer and guesses_left > 0:
print "The machine whirrs and clicks %s times." \
% guesses_left
guesses_left -= 1
elif guess != correct_answer and guesses_left == 0:
print "An alarm goes off and the police pick you up."
print "You are put on death row, because you live under a"
print "horrible dictatorship."
death()
else:
print "That ain't no key on the pad, yo."
def rm3(self):
def __init__(self):
if "coin" not in self.items:
print "You come into a room. A man stands at the doorway."
print "He tells you that in order to pass the door, you need to"
print "guess which hand holds the coin."
print "Do you try to guess?"
print "Obvious exits are east and north."
while True:
choice = inp()
if "guess" in choice:
return self.rm3guess
elif choice == "north":
return self.rm1
def rm4(self):
def __init__(self):
print "room 4"
raw_input
return self.rm4
def rm5(self):
pass
def rm6(self):
pass
game = Rooms()
运行并键入“south”时的输出为:
=== You are in Room 1 ===
You are in a dark room.
Obvious exits are east and south.
What do you do? south
Traceback (most recent call last):
File "ta.py", line 141, in <module>
game = Rooms()
File "ta.py", line 44, in __init__
current_room = current_room()
TypeError: 'NoneType' object is not callable
shell returned 1
==您在1号房间===
你在一个黑暗的房间里。
明显的出口在东部和南部。
你是做什么的?南方
回溯(最近一次呼叫最后一次):
文件“ta.py”,第141行,在
游戏=房间()
文件“ta.py”,第44行,在_init中__
当前房间=当前房间()
TypeError:“非类型”对象不可调用
壳返回1
我不确定这样的错误怎么会发生?为什么方法rm3在类中定义的时候是None类型?为什么要在函数中添加
\uuu init\uuu
函数?它是要添加到类中的构造函数。移除这些可以解决您的问题。我可以进入南部
并继续前进
from sys import exit
from random import randint
def death():
print 'You are "dead", I guess.'
exit(0)
def inp(prompt='What do you do? '):
to_return = raw_input(prompt).lower()
if to_return == 'q' or to_return == 'quit':
print "Bye now~"
exit(0)
else:
return to_return
class Rooms(object):
def __init__(self):
current_room = self.rm1
self.items = []
while True:
current_room = current_room()
def rm1(self):
print "=== You are in Room 1 ==="
print "You are in a dark room."
print "Obvious exits are east and south."
while True:
choice = inp()
if choice == "east":
return self.rm2
elif choice == "south":
return self.rm3
elif choice == "look":
return self.rm1
def rm2(self):
print "=== You are in Room 2 ==="
if "haircomb" not in self.items:
print "There is a high-tech looking keypad on the southernly wall,"
print "next to the door."
print "Upon closer inspection there are not numbers, but three"
print "letters on the keypad: A, M, and Z."
print "Do you try to guess the code?"
print "Obvious exits are west and south."
while True:
choice = inp()
if "guess code" in choice and "haircomb" not in self.items:
return self.rm2guess
elif choice == "west":
return self.rm1
elif choice == "south" and "haircomb" not in self.items:
print "The door is firmly locked."
elif choice == "south" and "haircomb" in self.items:
return self.rm4
elif choice == "look":
return self.rm2
def rm2guess(self):
correct_answer = randint(1,3)
guesses_left = 2
print "You approach the keypad."
while True:
guess = inp("Which key do you press? ").lower()
if guess == "a" or guess == "m" or guess == "z":
if guess == "a":
guess = 1
elif guess == "m":
guess = 2
elif guess == "z":
guess = 3
if guess == correct_answer:
print "The machine whirrs and then beeps."
print "The southernly door seems to be open now."
print "Suddenly, a haircomb falls from the bottom of the "
print "keypad. You take it."
self.items.append("haircomb")
return self.rm2
elif guess != correct_answer and guesses_left > 0:
print "The machine whirrs and clicks %s times." \
% guesses_left
guesses_left -= 1
elif guess != correct_answer and guesses_left == 0:
print "An alarm goes off and the police pick you up."
print "You are put on death row, because you live under a"
print "horrible dictatorship."
death()
else:
print "That ain't no key on the pad, yo."
def rm3(self):
if "coin" not in self.items:
print "You come into a room. A man stands at the doorway."
print "He tells you that in order to pass the door, you need to"
print "guess which hand holds the coin."
print "Do you try to guess?"
print "Obvious exits are east and north."
while True:
choice = inp()
if "guess" in choice:
return self.rm3guess
elif choice == "north":
return self.rm1
def rm4(self):
print "room 4"
raw_input
return self.rm4
def rm5(self):
pass
def rm6(self):
pass
game = Rooms()
为什么要在函数中添加
\uuuu init\uuu
函数?它是要添加到类中的构造函数。移除这些可以解决您的问题。我可以进入南部
并继续前进
from sys import exit
from random import randint
def death():
print 'You are "dead", I guess.'
exit(0)
def inp(prompt='What do you do? '):
to_return = raw_input(prompt).lower()
if to_return == 'q' or to_return == 'quit':
print "Bye now~"
exit(0)
else:
return to_return
class Rooms(object):
def __init__(self):
current_room = self.rm1
self.items = []
while True:
current_room = current_room()
def rm1(self):
print "=== You are in Room 1 ==="
print "You are in a dark room."
print "Obvious exits are east and south."
while True:
choice = inp()
if choice == "east":
return self.rm2
elif choice == "south":
return self.rm3
elif choice == "look":
return self.rm1
def rm2(self):
print "=== You are in Room 2 ==="
if "haircomb" not in self.items:
print "There is a high-tech looking keypad on the southernly wall,"
print "next to the door."
print "Upon closer inspection there are not numbers, but three"
print "letters on the keypad: A, M, and Z."
print "Do you try to guess the code?"
print "Obvious exits are west and south."
while True:
choice = inp()
if "guess code" in choice and "haircomb" not in self.items:
return self.rm2guess
elif choice == "west":
return self.rm1
elif choice == "south" and "haircomb" not in self.items:
print "The door is firmly locked."
elif choice == "south" and "haircomb" in self.items:
return self.rm4
elif choice == "look":
return self.rm2
def rm2guess(self):
correct_answer = randint(1,3)
guesses_left = 2
print "You approach the keypad."
while True:
guess = inp("Which key do you press? ").lower()
if guess == "a" or guess == "m" or guess == "z":
if guess == "a":
guess = 1
elif guess == "m":
guess = 2
elif guess == "z":
guess = 3
if guess == correct_answer:
print "The machine whirrs and then beeps."
print "The southernly door seems to be open now."
print "Suddenly, a haircomb falls from the bottom of the "
print "keypad. You take it."
self.items.append("haircomb")
return self.rm2
elif guess != correct_answer and guesses_left > 0:
print "The machine whirrs and clicks %s times." \
% guesses_left
guesses_left -= 1
elif guess != correct_answer and guesses_left == 0:
print "An alarm goes off and the police pick you up."
print "You are put on death row, because you live under a"
print "horrible dictatorship."
death()
else:
print "That ain't no key on the pad, yo."
def rm3(self):
if "coin" not in self.items:
print "You come into a room. A man stands at the doorway."
print "He tells you that in order to pass the door, you need to"
print "guess which hand holds the coin."
print "Do you try to guess?"
print "Obvious exits are east and north."
while True:
choice = inp()
if "guess" in choice:
return self.rm3guess
elif choice == "north":
return self.rm1
def rm4(self):
print "room 4"
raw_input
return self.rm4
def rm5(self):
pass
def rm6(self):
pass
game = Rooms()
您的
rm3
和rm4
函数没有任何有用的功能,因为出于某些原因,您在每个函数中都声明了嵌套函数\uuu init\uuu
因此,当您调用例如
rm3()
时,它并没有执行您所期望的操作,它声明了本地嵌套函数\uuuu init\uuuu
,并返回None
,因为它没有“自己的”主体 您的rm3
和rm4
函数没有任何有用的功能,因为出于某些原因,您在每个函数中都声明了嵌套函数\uuu init\uuu
因此,当您调用例如
rm3()
时,它并没有执行您所期望的操作,它声明了本地嵌套函数\uuuu init\uuuu
,并返回None
,因为它没有“自己的”主体 哦,天哪,我太笨了,对不起。我没有好的答案,除了我是个白痴上帝,我太愚蠢了,对不起。我没有好的答案,除了我只是个白痴