我下面有这段代码,看不到我在输入要显示的数据时做错了什么。我收到错误消息“游戏对象没有属性名称”。请帮助有人可以看到我在做什么错吗?(初学者程序员)
class games():
def _init_(self,name,platform,genre):
self.name = name
self.platform = platform
self.genre = genre
def display(self):
print (self.name,end='\t\t')
print (self.platform,end='\t\t')
print(self.genre)
def getData(games):
#st=list()
st = []
n=int(input("enter no of games: "))
print('games details are...')
for i in range (n):
print ('Games : ', i+1)
name=input('\tName : ')
platform=input('\tPlatform : ')
genre=input('\tGenre : ')
st.append(games())
print ("Game Details")
print ('Name\t\tPlatform\t\tGenre')
for i in range(n):
st[i].display()
getData(games)
一些更正:1.您在类初始化中有一个错字:__init__
,而不是_init_
。2.您将添加到游戏列表(st
)空实例。所以改变
st.append(games())
to
st.append(games(name,platform,genre))
另外,请为类和函数考虑适当的命名约定。参见PEP8 style guide for Class naming convention。从长远来看,它将为您提供帮助。