2D字典 – while True循环正在覆盖所有键值

huangapple go评论55阅读模式
英文:

2D Dictionary - while True Loop is overwriting all Key Values

问题

我正在使用while True和for循环的组合来生成一个2D Python字典,但是while循环会覆盖所有键的值,而不是最新的键值。请有人解释这种行为给我。

MokeDex = {}
MokeBeast = {"type": None, "specialmove": None, "startingHP": None, "startingMP": None}

while True:
    name = input("Input your beast's name >")
    for key in MokeBeast.keys():
        MokeBeast[key] = input(f"Input your beast's {key} >")

    MokeDex[name] = MokeBeast
    check = input("Do you want to add another beast? y/n>")
    if check == "n":
        break

如果我在while True循环中使用以下代码而不是(MokeDex[name]= MokeBeast),那么它会起作用:

type = input("Type > ").title()
hp = int(input("HP > "))
mp = int(input("MP > "))
mokedex[name] = {"type": type, "hp": hp, "mp": mp}
英文:

I am using a combination of while True and for loops to generate a 2D Python dictionary but the while loop is overwriting the values for all keys rather than the latest key value. Can someone please explain this behaviour to me.

'''

MokeDex = {}
MokeBeast = {"type":None, "specialmove":None,  "startingHP":None,"startingMP":None}

while True:
  name = input("Input your beast's name >")
  for key in MokeBeast.keys():
    MokeBeast[key] = input(f"Input your beast's {key} >")

  #print(MokeBeast)
  MokeDex[name]= MokeBeast
  #print(MokeDex)
  check = input("Do you want to add another beast? y/n>")
  if check == "n":
    break

#If I use the following snippet in the while True loop instead of (MokeDex[name]=      MokeBeast) then it works
 #type = input("Type > ").title()
 #hp = int(input("HP > "))
 #mp = int(input("MP > "))
 #mokedex[name] = { "type": type, "hp": hp, "mp": mp}

'''

答案1

得分: 1

以下是翻译好的部分:

这种行为出现的原因是因为Python处理对象引用的方式。在Python中,当你创建一个字典并将其分配给一个变量时,你并没有将字典存储在变量中,而是存储了对字典的引用。

在你的代码中,MokeDex[name]= MokeBeast 并没有将字典 MokeBeast 复制到 MokeDex[name] 中。它是将对 MokeBeast 的引用存储在 MokeDex 字典的键 name 下。这意味着每次你修改 MokeBeast 时,实际上是在修改 MokeDex 所有条目中引用的字典,因为它们都引用同一个字典。

要解决这个问题,你需要为每个野兽创建一个新的字典。你可以在循环内部复制字典来实现这一点:

MokeDex = {}
MokeBeast = {"type": None, "specialmove": None, "startingHP": None, "startingMP": None}

while True:
  name = input("输入你的野兽的名字 >")
  new_beast = MokeBeast.copy()
  for key in new_beast.keys():
    new_beast[key] = input(f"输入你的野兽的 {key} >")

  MokeDex[name] = new_beast
  check = input("是否要添加另一只野兽?是/否 >")
  if check == "否":
    break

在这段代码中,new_beast = MokeBeast.copy() 创建一个新的字典,它是 MokeBeast 的副本,所有的更改都针对 new_beast 进行。这确保了 MokeDex 中的每个野兽都是一个独立的字典。

英文:

The reason why you're seeing this behavior is because of how Python handles object references. In Python, when you create a dictionary and assign it to a variable, you're not storing the dictionary in the variable. Instead, you're storing a reference to the dictionary.

In your code, MokeDex[name]= MokeBeast isn't copying the dictionary MokeBeast into MokeDex[name]. It's storing a reference to MokeBeast under the key name in the MokeDex dictionary. That means that every time you modify MokeBeast, you're actually modifying the dictionaries referred to in all entries of MokeDex, because they all refer to the same dictionary.

To fix this, you'll need to create a new dictionary for each beast. You can do this by copying the dictionary inside the loop:

MokeDex = {}
MokeBeast = {"type":None, "specialmove":None,  "startingHP":None,"startingMP":None}

while True:
  name = input("Input your beast's name >")
  new_beast = MokeBeast.copy()
  for key in new_beast.keys():
    new_beast[key] = input(f"Input your beast's {key} >")

  MokeDex[name]= new_beast
  check = input("Do you want to add another beast? y/n>")
  if check == "n":
    break

In this code, new_beast = MokeBeast.copy() creates a new dictionary that's a copy of MokeBeast, and all changes are made to new_beast. This ensures that each beast in MokeDex is a separate dictionary.

huangapple
  • 本文由 发表于 2023年5月10日 19:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定