2017-12-27 204 views
-1

我一直在做一个记分员,我不知道如何分配玩家的变量数量为0.例如,如果有3名玩家,那么我需要分配3个不同的变量值这可能吗?如果是这样,怎么样?如果不是,我还能怎么做?Python记分

while True: 
    try: 
     numPlayers = int(input("How many people are playing?")) 
     if numPlayers == 0 or numPlayers == 1 or numPlayers > 23: 
      print("You cannot play with less than 2 people or more than 23 
     people.") 

     else: 
      break 

    except ValueError: 
     print("Please enter an integer value.") 

for numTimes in range(0, numPlayers): 
    #what should i do? 
+0

是什么阻止你使用了'list'? – RottenCandy

+0

使用'list'存储所有玩家的分数,例如:'scores = [0] * numPlayers'。然后'分数[0]'将成为第一名球员的得分,'得分[1]'第二名球员的得分...... – CristiFati

+0

谢谢!我会看看我是否可以使用列表 –

回答

0

使用字典,像这样:

players = {'player-{}'.format(num): 0 for num in range(1, num_players + 1)} 

也许从收藏defaultdict对象将符合甚至对这项任务更好:

from collections import defaultdict 

players = defaultdict(int) 
players['Dirk'] 
# Returns 0 
players['John'] += 1 
print(players) 
# Prints {'John': 1, 'Dirk': 0} 
+0

我也会利用字典,看看哪个更好! –