'Print both values in a dictionary without all the extra characters [duplicate]
I am making a text based game as one of my first projects, and want to print the different categories you can but a skill point in. I made a dictionary with the skill and its function in the game. I want to print both on the screen all together for example:
Health: This increases total health
That's what I have right now:
while True:
print("You will now choose a skill to level up")
skills = {
"Health": "This increases total health",
"Defense": "This increases damage negation",
"Luck": "This increases all chances",
}
for c in skills:
print(c)
break
I only know how to get it to print the first part of the dictionary. A solution would be appreciated, but if you could also explain it so I can get a better understanding that would also be great.
Solution 1:[1]
for c in skills: only iterates over the dictionary's keys.
You need to extract the key and value pairs instead:
while True:
print("You will now choose a skill to level up")
skills = {
"Health": "This increases total health",
"Defense": "This increases damage negation",
"Luck": "This increases all chances",
}
for key, value in skills.items():
print(key+ ": " + value)
break
This outputs:
You will now choose a skill to level up
Health: This increases total health
Defense: This increases damage negation
Luck: This increases all chances
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Kugelman |
