Search code examples
pythonfiletypeerrorgetopenfilename

Why am I getting this type error? TypeError: '>=' not supported between instances of 'str' and 'int'


I am a newbie student and trying to write a program that converts a list of raw scores to a list of letter grades. I need the loop, file open/close, if-elif-else statements, and 2 functions for the criteria of my assignment.

The file I'm opening for testing looks like this:

108
99
0
-1

Here's the program so far:

def convertscore(score):
    grade = ""
    if score >=101:
        print("Score is over 100%. Are you sure this is right?")
        grade = "A"
    elif score >=90:
        grade = "A"
    elif score >=80 <=89:
        grade = "B"
    elif score >=70 <=79:
        grade = "C"
    elif score >= 60 <=69:
        grade = "D"
    elif score >=0 <=59:
        grade = "F"
    elif score < 0:
        print("Score cannot be less than zero.")
    else:
        print("Unable to convert score.")

print(grade)


def main():
    print("This program creates a file of letter grades from a file of scores on a 100-point scale.")
    print()

    #get the file names
    infileName = input("What file are the raw scores in? ")
    outfileName = input("What file should the letter grades go in? ")

    #open the files
    infile = open(infileName, 'r')
    outfile = open(outfileName, 'w')

    #process each line of the output file
    for line in infile:
        #write to output file
        print(convertscore(line), file=outfile)

    #close both files
        infile.close()
        outfile.close()

    print()
    print("Letter grades were saved to", outfileName)

main()

If I try to run it, I get a type error:

Traceback (most recent call last):
  File "/Users/xxxx/Documents/convertscore.py", line 54, in <module>
main()
  File "/Users/xxxx/Documents/convertscore.py", line 45, in main
    print(convertscore(line), file=outfile)
  File "/Users/xxxx/Documents/convertscore.py", line 10, in convertscore
    if score >=101:
TypeError: '>=' not supported between instances of 'str' and 'int'

The convertscore program seems to work OK on its own so I'm confused. Thank you in advance for your help.


Solution

  • By default Python takes input as string , so you need to convert it as int And you need to return something from function or convertscore(line) will return null always. Below will work in python 2.7. Please check

    def convertscore(score):
        grade = ""
        if score >=101:
            grade = "Score is over 100%. Are you sure this is right? \n"
            grade += "A"
        elif score >=90:
            grade = "A"
        elif score >=80 <=89:
            grade = "B"
        elif score >=70 <=79:
            grade = "C"
        elif score >= 60 <=69:
            grade = "D"
        elif score >=0 <=59:
            grade = "F"
        elif score < 0:
            grade = "Score cannot be less than zero."
        else:
            grade = "Unable to convert score."
    
        return grade
    
    
    print("This program creates a file of letter grades from a file of scores on a 100-point scale.")
    #print()
    
        #get the file names
    infileName = input("What file are the raw scores in? ")
    outfileName = input("What file should the letter grades go in? ")
    
        #open the files
    infile = open(infileName, 'r')
    outfile = open(outfileName, 'w')
    
        #process each line of the output file
    for line in infile:
            #write to output file
        outfile.write(convertscore(int(line)))
        outfile.write("\n")
    
        #close both files
    infile.close()
    outfile.close()
    
    #print()
    print("Letter grades were saved to", outfileName)