Python3-while ids> Stop:TypeError:不可排序的类型:str()> int()

时间:2018-09-28 02:37:22

标签: python python-3.x for-loop error-handling while-loop

您好,我正在尝试运行一个程序,该程序将返回学生的成绩及其平均成绩。我还知道我是基本的第一年程序员。我可能会很担心。无论如何,请最好的帮助。 谢谢。

错误说是在第49行。

说“

line 49, in <module>
 while ids > STOP:
TypeErrorL unorderable types: str() > int()

XXXX

 def assigngrades(scores):
     avg = sum(scores)/len(scores)
     print(avg)


     for val in scores:
      if val > avg + 10:
       grade = 'A'
      elif val > avg + 5:
       grade = 'B'
      elif val > avg -5:
        grade = 'C'
      elif val > avg - 10:
       grade = 'D'
      else:
       grade = 'F'
      grades.append(grade)
     print("in assigngrades, grades: ",grades)
     return grades



    def printsummary(grades, ave):
     print('ID        Score        Average        Grade')
     print('===========================================')
     print( )


     for val in range(len(ids)):
      print('val', val)
      print(ids,' ',scores, ' ', grades)
     return

    #main
    ids = []
    scores = []
    grades = []
    STOP = 0


    ids = input("Enter an ID:")
    while ids > STOP:
     ids.append(ids)
     score = eval(input("Enter a score:"))
     scores.append(score)
     id = (input("Enter an ID number, 0 to STOP:"))

     grades = assigngrades(scores)
     print("after while loop")
     print("Ids:", ids, "Scores:", scores, "Grades:", grades)
     printsummary(grades, avg)

尽管如此,我对这个问题感到困惑。感谢您的宝贵时间,并帮助您解决此问题。非常感谢。是的,我知道我很傻。

1 个答案:

答案 0 :(得分:1)

input()函数返回一个字符串,因此应使用int()将其转换为整数,以便可以将其值与另一个整数进行比较。您还应该命名存储用户输入的变量,而不是ids,因为您已经将其定义为列表:

ids = []
scores = []
grades = []
STOP = 0

id = int(input("Enter an ID:"))
while id > STOP:
    ids.append(id)
    score = eval(input("Enter a score:"))
    scores.append(score)

    grades = assigngrades(scores)
    print("after while loop")
    print("Ids:", ids, "Scores:", scores, "Grades:", grades)
    printsummary(grades, avg)