I'm new to python and trying to sort scores in descendant order and see if who got the same score. Here's the code
Score = [90, 80, 92, 88, 75, 88, 80, 80, 90, 90, 90, 75]
Score.sort(reverse=True)
sorted_unique_score = []
#sorting in desc order
for score in Score:
if score not in sorted_unique_score:
sorted_unique_score.append(score)
#find the same scores
for unique_score in sorted_unique_score:
cnt = 0
for score in Score:
if unique_score == score:
cnt = cnt + 1 # somehow this one doesn't work --> cnt =+ cnt 1
else:
continue
if cnt == 1:
print(f"for {unique_score}: is a unique score")
else:
print(f"for {unique_score} : {cnt} students got the same score")
this works perfectly but I'm wondering why this one below doesn't work.
Score = [90, 80, 92, 88, 75, 88, 80, 80, 90, 90, 90, 75]
Score.sort(reverse=True)
sorted_unique_score = []
#sorting in desc order
for score in Score:
if score not in sorted_unique_score:
sorted_unique_score.append(score)
#find the same scores
for unique_score in sorted_unique_score:
cnt = 0
for score in Score:
if unique_score == score:
cnt =+ cnt 1
else:
continue
if cnt == 1:
print(f"for {unique_score}: is a unique score")
else:
print(f"for {unique_score} : {cnt} students got the same score")
the only difference is cnt =+ cnt +1 part
You should write like this: cnt += cnt 1 and not like : cnt =+ cnt 1