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

Question:

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
Asked By: HAZEM

||

Answers:

You probably need to specify that val is an integer:

gradedict[int(key)] = int(val)
Answered By: ML-Nielsen

The problem occurs in line #5:

with open("scorelist.txt") as f:
    for line in f:
       (key, val) = line.split()

In very simple terms, split() is basically a string method. So, when you do line.split(), "line" is considered as a string, and the split() function then separates the string into multiple parts based on the default separator, whitespace (or just a space). These separated components (which are still strings) get assigned to key and val.
In your scorelist.txt document, each line is considered a string, because each line contains a whitespace.

To resolve this, you can modify your code and specify that you want the integer form of val, since val still contains a string (even though that string is numeric).

 for line in f:
       (key, val) = line.split()
       gradedict[int(key)] = int(val)

What’s interesting is that you’ve already done this for key in your code, but you perhaps forgot to convert val into an int.

Answered By: MJK

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)
Answered By: Kiran S
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.