您好我在尝试遍历类列表时遇到一些麻烦,以便打印出该类实例所持有的特定值。我的程序编译但是它没有打印任何东西,我也尝试过在玩家列表中拥有" player.player"在for循环中,我也得到了相同的结果。非常感谢任何帮助。
我的主要功能
class main:
if __name__ == '__main__':
while True:
print("Please Select An Option Below:")
print("1 - Create Player:")
print("2 - List Players:")
print("3 - Create Team:")
print("4 - Sell Player:")
print("5 - Buy Player:")
x = raw_input("Please select a option: \n")
playerList = []
if(x == '1'):
import Player
p1 = Player.player()
pname = raw_input("Please Enter A Player Name \n")
page = raw_input("Please Enter A Player Age \n")
pwage = raw_input("Please Enter A Player Wage \n")
pteam = raw_input("Please Enter A Player Team \n")
p1.pname = pname
p1.page = page
p1.pwage = pwage
p1.pteam = pteam
playerList.append(p1)
continue
if(x == '2'):
for p1 in playerList:
print(p1.pname)
continue
我的播放器课程
class player(object):
@property
def pname(self):
return self.pname
@pname.setter
def pname (self, value):
pass
@property
def page(self):
return self.page
@page.setter
def page (self, value):
pass
@property
def pwage(self):
return self.pwage
@pwage.setter
def pwage(self, value):
pass
@property
def pteam(self):
return self.pteam
@pteam.setter
def pteam(self, value):
pass
---- -----更新
class main:
if __name__ == '__main__':
while True:
print("Please Select An Option Below:")
print("1 - Create Player:")
print("2 - List Players:")
print("3 - Create Team:")
print("4 - Sell Player:")
print("5 - Buy Player:")
x = raw_input("Please select a option: \n")
playerList = []
if(x == '1'):
import Player
nplayer = Player.player()
nplayer.set_name(raw_input("test\n"))
playerList.append(nplayer)
continue
if(x == '2'):
for player in playerList:
print(player.get_name)
continue
class player(object):
__name = ""
@property
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
答案 0 :(得分:0)
当你写p1.pname = pname
时,会调用setter。但是,您不会在setter中执行任何操作。因此,当稍后调用getter时,它没有任何东西可以返回。
要使getter和setter在python中工作,你需要做这样的事情:
class player(object):
@property
def pname(self):
return self._pname
@pname.setter
def pname (self, value):
self._pname = value
答案 1 :(得分:0)
非常感谢那些帮助它的人,我的问题是由于没有正确设置setter / getter而导致的,因为我在显示菜单选项中使用了我的数组,导致它将类的实例发送到另一个列表。 / p>
Identity