如何在将键更改为id_编号时将字典函数插入另一个字典函数?

2024-04-16 21:22:58 发布

您现在位置:Python中文网/ 问答频道 /正文

问题的目的是:

  • 实例化一个名为pokedex

  • 定义一个新函数,该函数将接受pokedex, poke_id, poke_name, poke_type

  • 使用my dictionary函数create_pokemon并向pokedex字典添加一个新的key:value对,其中keypoke_id,而value是新创建的口袋妖怪字典

我创建了一个dictionary函数:create_pokemon(poke_id, poke_name, poke_type)。键是'hp','defense', 'special_defense',列表还在继续

这是我的create_pokemon函数:


    new_pokemon= {

        'poke_id': poke_id,
        'poke_name': poke_name,
        'poke_type': poke_type,
        'hp': np.random.randint(400,501),
        'attack': np.random.randint(50,101),
        'defense': np.random.randint(50,101),
        'special_attack' : np.random.randint(100,151),
        'special_defense': np.random.randint(100,151),
        'speed': np.random.randint(0,101)      

    }```





so when I display pokedex, it should look something like:

```{1: {'attack': 64,
  'defense': 59,
  'hp': 495,
  'poke_id': 1,
  'poke_name': 'charmander',
  'poke_type': 'fire',
  'special_attack': 100,```

Tags: 函数nameidtypecreatenprandomhp