Python Forum
Line of code to show dictionary doesn't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line of code to show dictionary doesn't work
#1
Hi,

I am trying to write a line of code that should return the names and their favourite colors. Somehow it won't work. What am i doing wrong? I don't get an error messgae just nothing happens when i press enter after the code.

Thanks in advance!

>>> Colors = {"Sam": "Blauw", "Ali": "Rood", "Saar": "Geel"}
>>> Colors
{'Sam': 'Blauw', 'Ali': 'Rood', 'Saar': 'Geel'}
>>> Colors.keys()
dict_keys(['Sam', 'Ali', 'Saar'])
>>> for Item in Colors.keys():
	print("{0} houdt van de kleur {1}."
	      .format(Item, Colors{Item}))
	
SyntaxError: invalid syntax
>>> for Item in Colors.keys():
	print("{0} houdt van de kleur {1}."
	      .format(Item, Colors[Item]))
Reply
#2
Did you forget to indent the print inside the for? Also, a few things:

1. Dictionaries are iterable and iterating over them gives you the keys, so calling keys explicitly is unnecessary.

2. If you want both the key and the value, you can use items:

>>> d = {"foo": 1, "bar": 2}
>>> for key, value in d.items():
...     print(key, value)
... 
foo 1
bar 2
Reply
#3
Use Colors['Sam'] to get Sam's favorite color. You can also use Colors.get('Sam'). get is useful when looking up keys that may not be in the dictionary. Colors['Bob'] will throw an exception, but Colors.get('Bob', 'Green') will return 'Green', a default value used when the key is not found.

This code can use [] because the keys are guaranteed to be in the dictionary.
Colors = {'Sam': 'Blauw', 'Ali': 'Rood', 'Saar': 'Geel'}

for Item in Colors:
    # print("{0} houdt van de kleur {1}.".format(Item, Colors[Item])) # Works
    print(f'{Item} houdt van de kleur {Colors[Item]}.')) # Works, shorter, reads better
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code used to work 100%, now sometimes works! muzicman0 5 465 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 172 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 228 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Something the code dont work AlexPython 13 601 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  Why doesn't it show me anything in print? Melcu54 2 247 Oct-01-2022, 12:07 AM
Last Post: snippsat
  PIL Image im.show() no show! Pedroski55 2 352 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  cannot get code to work Led_Zeppelin 10 1,011 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 754 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 1,841 May-30-2022, 03:31 PM
Last Post: bowlofred
  How does this code work? pd_minh12 3 629 Apr-15-2022, 02:50 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020