1# Creates key, value for dict based on user input
2# Combine with loops to avoid manual repetition
3class_list = dict()
4data = input('Enter name & score separated by ":" ')
5temp = data.split(':') class_list[temp[0]] = int(temp[1])
6
7OR
8
9key = input("Enter key")
10value = input("Enter value")
11class_list[key] = [value]
1>>> fruits = {}
2>>> key1 = input("Enter first key for fruits:")
3Enter first key for fruits:a
4>>> value1 = input("Enter first value for fruits:")
5Enter first value for fruits:apple
6>>> key2 = input("Enter second key for fruits:")
7Enter second key for fruits:b
8>>> value2 = input("Enter second value for fruits:")
9Enter second value for fruits:banana
10>>> fruits[key1] = value1
11>>> fruits
12{'a': 'apple'}
13>>> fruits[key2] = value2
14>>> fruits
15{'a': 'apple', 'b': 'banana'}
16>>># Same variable names for keys and values can be used if used inside a loop like this
17>>> fruits.clear()
18>>> fruits
19{}
20>>> for i in range(2):
21 key = input("Enter key for fruits:")
22 value = input("Enter value for fruits:")
23 fruits[key] = value
24
25Enter key for fruits:a
26Enter value for fruits:apple
27Enter key for fruits:b
28Enter value for fruits:banana
29>>> fruits
30{'a': 'apple', 'b': 'banana'}
1class_list = dict() data = input('Enter name & score separated by ":" ') temp = data.split(':') class_list[temp[0]] = int(temp[1]) # Displaying the dictionary for key, value in class_list.items(): print('Name: {}, Score: {}'.format(key, value))