Python记分

我一直在做一个记分员,我不知道如何分配玩家的变量数量为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?

回答:

使用字典,像这样:

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}

以上是 Python记分 的全部内容, 来源链接: utcz.com/qa/266163.html

回到顶部