-2

I am trying to return the letter grade for a list of test scores. It keeps saying

'>=' not supported between instances of 'list' and 'int'

Here is the code:

g = [87, 92, 100, 54, 72, 84, 81, 74]


def grade(g):
    if ([g]>=90):
        return "A"
    elif (80<=[g]<90):
        return "B"
    elif (70<=[g]<80):
        return "C"
    elif (60<=[g]<70):
        return "D"
    elif ([g]<60):
        return "F"
for i in range(len(g)):
    print(grade((g)))
khelwood
  • 55,782
  • 14
  • 81
  • 108
Gavin
  • 113
  • 5
  • 1
    So why are you comparing a `list` with an `int`? – blhsing Mar 03 '20 at 20:08
  • 2
    Did you consider *not* putting the value in a list? Also it's a bad idea to shadow names like that. – jonrsharpe Mar 03 '20 at 20:08
  • not sure, just trying to figure out how to make it work – Gavin Mar 03 '20 at 20:08
  • 1
    In this case, the error message is telling you exactly what the problem is - you're trying to compare a list to an integer, which doesn't make sense. Remove the brackets - e.g. replace `if ([g]>=90):` with `if (g>=90):` – EJoshuaS - Stand with Ukraine Mar 03 '20 at 20:09
  • I understand, but how do I make a code that will return the letter grade of each value in the list? – Gavin Mar 03 '20 at 20:09
  • You need to iterate over each list item, or [map](https://docs.python.org/3/library/functions.html#map) the function to the list – G. Anderson Mar 03 '20 at 20:11
  • Please provide the entire error message. – AMC Mar 03 '20 at 20:30
  • 1
    Similar and duplicate questions: https://stackoverflow.com/questions/43403992/typeerror-not-supported-between-instances-of-list-and-int, https://stackoverflow.com/questions/49472108/typeerror-not-supported-between-instances-of-list-and-int/49472188 – AMC Mar 03 '20 at 20:32

3 Answers3

2

The problem is that putting brackets around g makes it a list. It's a list with exactly one item in it, but it's still a list nonetheless.

The runtime is telling you that it doesn't make sense to compare those two kinds of items - you're doing an apples-to-oranges comparison.

Remove the brackets around g. You should also name the local variable something other than g (because you're shadowing the variable). For example, replace if ([g]>=90): with if (grade>=90):

So your code should be something like:

# Give this a more descriptive name
grades = [87, 92, 100, 54, 72, 84, 81, 74]

# Rename 'g' to something that's different than the list name and is also more descriptive
# Also, since we're calling the variable name 'grade' now, I renamed
# the method 'letterGrade' to avoid confusion
def letterGrade(grade):
    # Make 'grade' NOT be a list
    if (grade >=90):
        return "A"
    elif (80<=grade <90):
        return "B"
    #etc.
  • 1
    Thank you very much! – Gavin Mar 03 '20 at 20:20
  • @Gavin Glad I could help. By the way, can you [accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if it solved the problem? – EJoshuaS - Stand with Ukraine Mar 03 '20 at 20:25
  • Note that python2 allows comparing a list to an int [1]. But it is probably not doing what you want - and certainly not what the OP was expecting. And it seems the OP was likely using python3 anyway (p.s., be specific in your questions - including python version) [1] https://stackoverflow.com/questions/3270680/how-does-python-2-compare-string-and-int-why-do-lists-compare-as-greater-than-n – Juan Mar 03 '20 at 21:14
0

As i understood you want it:

def grade(g):
  if (g>=90):
      return "A"
  elif (80<=g<90):
    return "B"
  elif (70<=g<80):
    return "C"
  elif (60<=g<70):
    return "D"
  elif (g<60):
    return "F"

g = [87, 92, 100, 54, 72, 84, 81, 74]

for i in range(len(g)):
  print(grade((g[i])))

your code is not working because you give list as parametr to a function.

  • Thank you, I think it also worked to map the function to the list. I will try this way also! Thank you! – Gavin Mar 03 '20 at 20:18
  • Your code worked perfectly as well. Thank you again. – Gavin Mar 03 '20 at 20:19
  • Run this with python2, and no error! And the students all love you because they all get an A. https://stackoverflow.com/questions/3270680/how-does-python-2-compare-string-and-int-why-do-lists-compare-as-greater-than-n – Juan Mar 03 '20 at 21:19
-1
def grade(g):
    if (g>=90):
        return "A"
    elif (80<=g<90):
        return "B"
    elif (70<=g<80):
        return "C"
    elif (60<=g<70):
        return "D"
    elif (g<60):
        return "F"

g = [87, 92, 100, 54, 72, 84, 81, 74]

Scores = map(grade,g)
print(list(Scores))

This seemed to fix the problem! Mapping the function to the list. Thank you guys for the help!

Gavin
  • 113
  • 5