Why i have an error when I try to print the second item in the next code? (Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why i have an error when I try to print the second item in the next code? (Python)

house = {red: 12 , blue: 14} print (house[house[2]])

4th Aug 2020, 8:08 PM
Unreal toons
Unreal toons - avatar
4 Answers
+ 3
Jan Markus nope we're both wrong I ran the code and it was a name error unless that's what you're saying and I'm just really dumb
4th Aug 2020, 8:26 PM
PresidentOfYes12
PresidentOfYes12 - avatar
+ 2
Array or dictionary indexes start with 0, so the second would be the index 1.
4th Aug 2020, 8:11 PM
PresidentOfYes12
PresidentOfYes12 - avatar
+ 1
Rrr Is that your total code? You defined house as dictionary.. and also wrong keys ,that red,blue are not defined... may be you mean "red", "blue" .. strings. It is not an array .. so you should access it by its keys not indexes.. edit: house = {"red": 12 , "blue": 14} print (house["blue"])
4th Aug 2020, 8:23 PM
Jayakrishna 🇮🇳
0
Then only i can look for the keys for the name and not for the index?
4th Aug 2020, 9:31 PM
Unreal toons
Unreal toons - avatar