我一直在使用python 3.2中的纸牌游戏,我不确定如何实例化单独的玩家。游戏可以有2个玩家到8个玩家。我以为我可以使用蛮力,并有类似的东西:
players = int(input('how many players?: ))
if players ==2:
p1 = Player()
p2 = Player()
elif players ==3:
p1 = Player()
p2 = Player()
p3 = Player()
elif players ==4:
p1 = Player()
p2 = Player()
p3 = Player()
p4 = Player()
等...
好像很脏。是否有一种更清洁的方式解决这个问题?谢谢。
答案 0 :(得分:1)
使用列表和for循环:
players = int (input ('how many players?: '))
if not 2 <= players <= 8:
<raise an exception or something>
p = []
for _ in range (players):
p.append (Player())
现在你有了一个你可以做你喜欢的球员名单。