-1

Brand new to python. When using the map function in python, i get a series of the word "none" as an output when i run my function. ive added screenshot below.

Code

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

def convertRawsToLetters(myList):
   return list(map(rawGradeToLetter, myList))

Example run:

>>> convertRawsToLetters([90,80,70])
A
B
C
[None, None, None]

I have a feeling it has something to do with the word list before the map function, but if I remove that all I get is the address of the map like "map object at 0x8g9b7ea51950".

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Your function doesn't return anything. See https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-than-printing-it – c2huc2hu Nov 13 '17 at 19:45
  • In addition to the problem of needing to return rather than print, note that `map` isn't used very often in Python since it can almost always be replaced by a more readable list comprehension. – John Coleman Nov 13 '17 at 19:52

3 Answers3

4

You need to return the grades, not print them. Printing the grades displays them on screen instead of giving a value back to the caller.

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

Without a return statement your function implicitly returns None, which explains why you're seeing [None, None, None] in your output.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Note: The final `elif` could just be made an `else`, with the only difference in behavior occurring for "weirdo" objects like `float('nan')` which return `False` for all comparisons. – ShadowRanger Nov 13 '17 at 19:47
0

I think it is because you aren't returning anything on your map function, so it doesn't work correctly. You can try this, so the instead of being mapped to None, it will be mapped to the results:

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

def convertRawsToLetters(myList):
   return list(map(rawGradeToLetter, myList))

It is, if you want to maintain the prints, if not, you can remove them.

-1
def rawGradeToLetter(grade):
    if grade >= 90:
        print ("A")
    elif grade < 90 and grade >= 80:
        print ("B")
    elif grade < 80 and grade >= 70:
        print ("C")
    elif grade < 70 and grade >= 60:
        print ("D")
    elif grade < 60:
        print ("F")


def convertRawsToLetters(myList):
    for grade in myList:
        rawGradeToLetter(grade)

I think you are over complicating it by using map() if all you want to do is print the grades.

Jacobjanak
  • 307
  • 1
  • 2
  • 10
  • @wpercy Yeah, I was looking at someone else's code for the rawGradeToLetter function. The OP doesn't actually need a list though and I think that's what you're talking about. OP just wants to print. – Jacobjanak Nov 13 '17 at 19:59