-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 3 question 6.py
58 lines (51 loc) · 1.48 KB
/
Assignment 3 question 6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
print("\nQuestion 6")
repeat="Y"
dic={}
dic2={}
#Y or N
liste=["Y","y","n","N"]
while repeat=="Y" or repeat=="y":
name = str(input("Enter student name:"))
sid = int(input("Enter student SID:"))
if sid<0:
print("\nError\nSID can't be negative\n")
else:
dic.update({sid: name})
dic2.update({name:sid})
repeat = str(input("Enter Y to continue or N to end:"))
if repeat=="N" or repeat=="n":
break
elif (not (repeat in liste)):
print("\nError\nPlease enter valid input['Y' or 'N']")
repeat=str(input("\nEnter Y to continue or N to end:"))
# a
#printing the dictionary
print("\nQ.6(a)")
print("The Dictionary containing {'SID':'Name'} is shown below")
print(dic)
# b
#sorting according to name
print("\nQ.6(b)")
list_k=sorted(dic2)
dic3={}
for ele in list_k:
dic3.update({dic2.get(ele):ele})
print("The Dictionary after sorting according to name:")
print(dic3)
# c
#sorting according to SID
print("\nQ.6(c)")
sort_dic = sorted(dic)
dic4 = {}
for va in sort_dic:
dic4.update({va: dic.get(va)})
print("The Dictionary after sorting according to SID:")
print(dic4)
# d
print("\nQ.6(d)")
# Taking input SID to be searched
enter_sid = int(input("Enter SID to get name of student:"))
# Searching for sid as key in dic
output_name = str(dic.get(enter_sid))
# printing name with key sid
print(f"Name of student with SID {enter_sid} is {output_name}.")