Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I'm getting that I have a strings while I'm working with integers?

Hello guys I’m new to python and I’m trying to solve this exercise
where I have a student IDs and their score in a file and I should make the dictionary whose keys are the ID and values are grade and then print the frequency of the grades (ex: A:7 etc..)
score and grade mappings are as follows: — 90 – 100 : A — 80 – 89 : B — 70 – 79 : C — 60 – 69 : D — 0 – 59 : E
I’m working with integers only but I’m still getting the error (‘>=’ not supported between instances of ‘str’ and ‘int’)so what’s wrong with my code?

from collections import defaultdict
gradedict = {}
with open("scorelist.txt") as f:
    for line in f:
      (id, score) = line.split()
      gradedict[int(id)] = int(score)
for id,score in gradedict.items():
  if val>=90:
    print(id,score,"A")
  elif 80<score<89:
    print(id,score,"B")
  elif 70<score<79:
    print(id,score,"C")
  elif 60<val<69:
    print(id,score,"D")
  elif 0<score<59:
    print(id,score,"E")
print(gradedict)
frequency = {}
for grade in ['A','B','C','D','E']:
  frequency[grade] = ['A','B','C','D','E'].count(grade)
print(frequency)

and here is the inside of the file

121787 74
121367 71
121817 88
121619 85
131445 80
131244 96
131872 98
131963 75
131172 78
131965 72
131112 90
131956 87
141105 61
141703 61
141407 78
141569 82
141585 89
141455 82
141370 80
141837 67
141857 86
141497 94
141853 67
141245 80
151452 83
151238 62
151827 58
151409 40
151789 95
151742 71
151133 40
151095 49
151186 75
151586 51
151926 73
151975 96
151079 49
151091 100
151588 49
151630 61

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Check if this is working,replace val with score

 from collections import defaultdict
    ...: gradedict = {}
    ...: try:
    ...:   with open("scorelist.txt") as f:
    ...:     for line in f:
    ...:       (id, score) = line.split()
    ...:       gradedict[int(id)] = int(score)
    ...: except:
    ...:     pass
    ...: for id,score in gradedict.items():
    ...:   if score>=90:
    ...:     print(id,score,"A")
    ...:   elif 80<score<89:
    ...:     print(id,score,"B")
    ...:   elif 70<score<79:
    ...:     print(id,score,"C")
    ...:   elif 60<score<69:
    ...:     print(id,score,"D")
    ...:   elif 0<score<59:
    ...:     print(id,score,"E")
    ...: print(gradedict)
    ...: frequency = {}
    ...: for grade in ['A','B','C','D','E']:
    ...:   frequency[grade] = ['A','B','C','D','E'].count(grade)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading