Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError
#1
Hello all, new to Python and programming and having an issue here that I can't figure out. I'm getting a value error from this code. I'm trying to print out their names neatly and their favourite numbers, very simple.

Any help appreciated, thanks!

favourite_numbers = {
	'mara': ['0', '51'],
	'jared': ['1', '73'],
	'stranger1': ['2', '61'],
	'stranger2': ['3', '9053'],
	'stranger3': ['4', '7'],
	}
#looping through dictionary favourite_numbers	
for name, number in favourite_numbers:
	print(name.title() + "'s favourite numbers are: ")
	for nums in number:
		print("\t" + nums)
Reply
#2
If you want to iterate of the keys and values of the dictionary at the same time, you need to use the items method:

for name, number in favourite_numbers.items():
Default iteration over a dictionary just returns the keys.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Gah!! I can't believe I didn't notice it! Haha. Thanks!! All my other code has the items and I was using it as a reference because it had worked but I somehow missed the .items.
Reply


Forum Jump:

User Panel Messages

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