Python 如何在同一类的另一个函数AttributeError:';刽子手';对象没有属性';word#u商店';
您必须在所有函数中使用Python 如何在同一类的另一个函数AttributeError:';刽子手';对象没有属性';word#u商店';,python,class,attributeerror,Python,Class,Attributeerror,您必须在所有函数中使用self.来创建和使用类变量 class Hangman: #class hangman def Word_list(self): result=random.choice(['debtee', 'mob', 'freight', 'torment', 'freight train', 'boredom','botcher', 'bothersome', 'bungler', 'continuation', 'officially', 'pract
self.
来创建和使用类变量
class Hangman: #class hangman
def Word_list(self):
result=random.choice(['debtee', 'mob', 'freight', 'torment', 'freight train', 'boredom','botcher', 'bothersome', 'bungler', 'continuation', 'officially', 'practically'])
word_len=len(result)
word_store=list(result)
def main_game(self):
#print('The word is %s long'%Word_list.list_result_len)
spaces=[]
for space in range(1,10):
spaces.append('_ ')
chance=10
print(spaces)
for x in range(1,10):
choose =input('Kindly choose a letter that you think this word contains :')
if choose in self.word_store: #facing the issue here
position=self.word_store.index(choose)
spaces[position]= choose
print(spaces)
print("Great!! This letter is present in the word, Keep Going")
else:
chance=chance-1
print("Sorry this letter does not exist in the word")
hangman_figure(chance)
对代码提出质疑,而不是评论。当您编辑问题时,请选择代码并使用按钮
{}
正确格式化代码。始终将问题中的完整错误消息(从单词“Traceback”开始)作为文本(而不是屏幕截图)放置。还有其他有用的信息。请在问题正文中描述问题,而不是在标题中class
您必须使用self.word\u store
才能访问所有函数中的变量。您应该在任何关于Python类和面向对象编程(OOP
)的教程中学习它。谢谢@furas的建议。但是你可以看到我已经在我的函数中使用self.word\u store了。我不知道我在课堂上做错了什么。
self.word_store = list(result)