Python 3- assigns grades [duplicate]

泪湿孤枕 提交于 2019-12-13 10:56:48

问题


• 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()

回答1:


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.



来源:https://stackoverflow.com/questions/36558696/python-3-assigns-grades

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!