Python 3- Assign grade

后端 未结 2 1605
别跟我提以往
别跟我提以往 2021-01-29 14:00

I am trying to write a program that reads a list of scores and then assigns a letter grade based on the score. Define a function to prompt user to enter valid scores until they

2条回答
  •  时光取名叫无心
    2021-01-29 14:40

    In this piece of code, you're not adding the value entered by the user to the list grade; you're re-defining grade so it's no longer a list but instead an integer:

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

    This wouldn't fly in strict, type-safe languages but, hey, it's python :)

    To fix it, rename the list (grades would be an intuitive name) and use append() to add the value of grade to it. Then, return grades, not grade to get a list back.

提交回复
热议问题