Search code examples
pythonpython-3.xfor-loopprogram-entry-point

Python 3- assigns grades


• Define a function to prompt the user to enter valid scores until they enter a sentinel value -999. Have this function both create and return a list of these scores. Do not store -999 in the list! • Have main( ) then pass this the list to a second function which traverses the list of scores printing them along with their appropriate grade.

I am having trouble with the getGrade function, it gives the error for i in grades: name 'grades' is not defined.

def main():
   grade = getScore()
   print(getGrade(grade))

def getScore():
   grades = []
   score = int(input("Enter grades (-999 ends): "))
   while score != -999:
      grades.append(score)
      score = int(input("Enter grades (-999 ends): "))
   return grades

def getGrade(score):
   best = 100
   for i in grades:
      if score >= best - 10:
         print(" is an A")
      elif score >=  best - 20:
         print(score, " is a B")
      elif score >= best - 30:
         print(score, " is a C")
      elif score >= best - 40:
         print(score, " is a D")
      else:
         print(score, "is a F")

main()

Solution

  • You defined your function to be getScore() but you're calling getScores(), so as would be expected this throws an error because the function you're calling doesn't actually exist / hasn't been defined.

    Addendum: Since you changed your question, after fixing the previous error.

    Likewise you're calling grades, but grades is defined in your other function not within the scope of the function where you're trying to iterate over grades.