-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2.py
68 lines (54 loc) · 1.99 KB
/
q2.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
59
60
61
62
63
64
65
66
#Q2 Labfinalpractice
ns = {}
def NameandScore():
while True:
inp = input("Enter student name and score: ")
if inp == "end" or inp == "end 0":
break
name,score = inp.split()
if 100 >= int(score) >= 0:
if name not in ns:
ns[name] = int(score)
else:
print("Duplicated name!")
else:
print("Invalid score!")
def SortedScore():
sorted_score = sorted(ns.values(), reverse =True) #To create list of sorted score from high to low
for i in sorted_score:
if sorted_score.count(i) != 1: #delete the duplicated value to avoid printing the duplicated condition in next for loop
sorted_score.remove(i) #remove until it has 1 left so that the for loop below can avoid duplicated conditions when we have more than 1 same score Ex. having [80,60,60]
print("List:")
if len(sorted_score) > 0:
for i in sorted_score: #check for each score
for k,v in ns.items():
if i == v: #To print only the condition of that score equal to that value in the dictionary and also able to print key (For printing value while able to print key)
#Ex. having [80,60,60] will get us duplicated output in '60' condition
print("%-10s %s"%(k,v))
else:
print("> empty list!")
NameandScore()
SortedScore()
#P'Time's solution
#Using itemgetter
from operator import itemgetter
def print_sorted(dict):
for key,value in sorted(dict.items(),key=itemgetter(1),reverse=True):
print("%s \t %d"%(key,value))
students={}
while True:
name,score=input("Enter student name and score: ").split()
if name=="end" and score=="0":
break
score=int(score)
if score<0 or score>100:
print("Invalid score!")
elif name not in students:
students[name]=score
else:
print("Duplicated name!")
print("List: ")
if len(students)==0:
print("> empty list!")
else:
print_sorted(students)