gpt4 book ai didi

python - 回合结束后数值被覆盖,打印时可见

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:26 25 4
gpt4 key购买 nike

我的代码:

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}]]

最佳答案

Python 中的赋值语句不会复制对象,而是在目标和对象之间创建绑定(bind)。您必须使用深层复制来复制用户列表

有一个模块复制您可以将其用作

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)

关于python - 回合结束后数值被覆盖,打印时可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41362116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com