Question #100904

Create a Python dictionary that returns a list of values for each key. The key can be whatever type. Design the dictionary so that it could be useful for something meaningful. Create at least three different items in it.

Expert's answer

I created two versions, the first without keyboard input, and the second with keyboard input.


#version 1
dictionary = {"Bob": 16, "Mark": 15, "Elena": 17, "Tom": 13}

print("Age of my friends:\n")

for key in dictionary:
	print(key, "-" ,str(dictionary[key]), "y.o.")


#version 2
dictionary = {}
name = input("Enter dictionary name: ")
key = None
while True:
	key = input("Enter the key (0 if you want to finish filling): ")
	if key != "0":
		value = input("Enter the value: ")
		dictionary[key] = value
	else:
		break

print("\n" + name + ":\n")
for key in dictionary:
	print(key, "-" ,str(dictionary[key]))
LATEST TUTORIALS
APPROVED BY CLIENTS