Yahtzee,用类attribu替换全局列表

2024-05-29 08:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个yahtzee游戏作为学校作业,目前我在我的一个函数中使用一个全局列表,并被告知这是不允许的。我正在尝试找出如何替换函数中的全局列表:

def welcome():
   global spelarlista
   spelarlista=[]
   print("Welcome to the yahtzee game!")
   players = int(input("How many players: "))
   rounds=0
   while not players==rounds:
       player=input("What is your name?: ")
       rounds=rounds+1
       spelarlista.append(Player(player))

在类播放器中具有类属性。现在,player类是这样的:

class Player:
    def __init__(self,name):
        self.name=name
        self.lista={"aces":0,"twos":0,"threes":0,"fours":0,"fives":0,"sixes":0,"upper score":0,"bonus":0,"pair":0,"two pairs":0,"3 of a kind":0,"4 of a kind":0,"small straight":0,"large straight":0,"full house":0,"chance":0,"yatzy":0,"sum":0}
        self.upper={"aces":0,"twos":0,"threes":0,"fours":0,"fives":0,"sixes":0}

到目前为止,我在Player类中的几个方法中使用了global spelarlista,我希望不必全部替换它。有没有办法用我可以从player类调用的东西来替换全局spelarlista?你知道吗


Tags: 函数nameself列表inputdef全局global

热门问题