轮次播放后覆盖的值,打印时可见

时间:2016-12-28 12:24:51

标签: python

我的代码:

users = []
users.append({"name" : "",
              "numerics" : [],
              "score" : 0 })
users[0]["name"] = input("Your name plz: ")

def getNumbers():
    for i in range(len(users)):
        numbers = input("Assign 8 different numbers (separate each with a comma ','): ")
        userNumbers = numbers.split(",")
    return userNumbers

users[0]["numerics"] = getNumbers()

scores = []
scores.append(users[:])
print(scores)

users[0]["numerics"] = getNumbers()
scores.append(users[:])

print(scores)

正在运行的示例:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> users = []
>>> users.append({"name" : "",
              "numerics" : [],
              "score" : 0 })
>>> users[0]["name"] = input("Your name plz: ")
Your name plz: Gladiator
>>> def getNumbers():
    for i in range(len(users)):
        numbers = input("Assign 8 different numbers (separate each with a comma ','): ")
        userNumbers = numbers.split(",")
    return userNumbers

>>> users[0]["numerics"] = getNumbers()
Assign 8 different numbers (separate each with a comma ','): 99,23,55,11,19,100,1000,89
>>> scores = []
>>> scores.append(users[:])
>>> print(scores)
[[{'numerics': ['99', '23', '55', '11', '19', '100', '1000', '89'], 'name': 'Gladiator', 'score': 0}]]
>>> users[0]["numerics"] = getNumbers()
Assign 8 different numbers (separate each with a comma ','): 100,56,77,32,99,22,45,2
>>> scores.append(users[:])
>>> print(scores)
[[{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'], 'name': 'Gladiator', 'score': 0}], [{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'], 'name': 'Gladiator', 'score': 0}]]
>>> 

我希望能够为游戏中的每一轮新功能,对用户进行更改数字并得分,并将其附加到得分列表然后打印那个。但是,我现在的方式似乎只是覆盖数字得分但主要是数字部分。

这根本不起作用:users[0]["score"].append(getNumbers()) 或者我应该只清除列表中的值,例如每个新一轮玩家的得分和数字。

Round 1: 
Player: Gladiator
Numerics: ['99', '23', '55', '11', '19', '100', '1000', '89']

Round 2: 
Player: Gladiator
Numerics: ['100', '56', '77', '32', '99', '22', '45', '2']

但打印出来显示:

[[{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'], 
'name': 'Gladiator', 'score': 0}], 

[{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'], 
'name': 'Gladiator', 'score': 0}]]

编辑:我尝试重置数字列表:

 users[0]['numerics'] = []
TypeError: list indices must be integers or slices, not tuple

测试由@Sarathsp

运行代码示例
Your name plz: Gladiator
Assign 8 different numbers (separate each with a comma ','): 99,100,1000,4,66,77,11,9
[[{'name': 'Gladiator', 'numerics': ['99', '100', '1000', '4', '66', '77', '11', '9'], 'score': 0}]]
Assign 8 different numbers (separate each with a comma ','): 100,33,44,55,0,1,9,2
[[{'name': 'Gladiator', 'numerics': ['99', '100', '1000', '4', '66', '77', '11', '9'], 'score': 0}], [{'name': 'Gladiator', 'numerics': ['100', '33', '44', '55', '0', '1', '9', '2'], 'score': 0}]]

1 个答案:

答案 0 :(得分:1)

Python中的赋值语句不复制对象,它们在目标和对象之间创建绑定。您必须使用deep copy复制users列表

有一个模块copy 你可以用它作为

copy.deepcopy(users)

以下是您的计划的样子

import copy
users = []
users.append({"name" : "",
              "numerics" : [],
              "score" : 0 })
users[0]["name"] = input("Your name plz: ")

def getNumbers():
    for i in range(len(users)):
        numbers = input("Assign 8 different numbers (separate each with a comma ','): ")
        userNumbers = numbers.split(",")
    return userNumbers

users[0]["numerics"] = getNumbers()

scores = []
scores.append(copy.deepcopy(users))
print(scores)

users[0]["numerics"] = getNumbers()
scores.append(copy.deepcopy(users))

print(scores)