This is a question about the double for door

Asked 2 years ago, Updated 2 years ago, 14 views

key_list = ["name", "hp", "mp", "level"]
value_list = ["Articles", 200, 30, 5]
character = {}

for key in key_list:
    for value in value_list:
        character[key] = value

print(character)

I entered it like this.

The result value is {'name': 'article', 'hp': 200, 'mp': 30, 'level':5}
I want to come out like this.
The book says to use the string length I'd like to print it out using a double for statement.
But if you run that code, the keys go in order well, but I wonder why the value is only 50,000 for each key.
Also, please make it look like the result.

python

2022-09-21 19:17

1 Answers

for key, value in zip(key_list, value_list):

You can do it like this.

The problem with the question code can be easily understood by turning it around as below.

for key in key_list:
    for value in value_list:
        character[key] = value
        print(character)


2022-09-21 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.