代码之家  ›  专栏  ›  技术社区  ›  jumon chowdhury

TypeError:python中/:“str”和“int”的操作数类型不受支持

  •  0
  • jumon chowdhury  · 技术社区  · 6 年前

    我正在做一个项目,寻找学生三次考试的平均成绩。

    我的工作似乎一切都很好,但有一个错误我收到了以下行:

    student_avg = ((firstterm) +str (midterm) +str (final)) / 3 
    

    该程序是讲师的配套应用程序。我的教授告诉我要做5门科目的第一学期期中和期末考试。

    工作一周后,我在这方面仍然面临一些问题。 有人能检查一下我的代码,看看我的错误是什么吗?

    def determine_score(grade):
        if 95<= grade <=100:
            return 'A+'
        elif 91<= grade <=96:
            return 'A'
        elif 89<= grade <=92:
            return 'A-'
        elif 85<= grade <=90:
            return 'B+'
        elif 81<= grade <=86:
            return 'B'
        elif 79<= grade <=82:
            return 'B-'
        elif 75<= grade <=80:
            return 'C+'
        elif 72<= grade <=76:
            return 'C'
        elif 64<= grade <=73:
            return 'D'
        elif 0<= grade <=65:
            return 'F'
        else:
            return 'invalide score'
    
    def firstTerm():
        fst_sub1=int(input("Enter first term marks of the first subject: "))
        fst_sub2=int(input("Enter first term marks of the second subject: "))
        fst_sub3=int(input("Enter first term marks of the third subject: "))
        fst_sub4=int(input("Enter first term marks of the fourth subject: "))
        fst_sub5=int(input("Enter first term marks of the fifth subject: "))
        firsttermScore = (fst_sub1+fst_sub2+fst_sub3+fst_sub4+fst_sub5)/5
        return 'firsttermScore'
    
    def midTerm():
        mid_sub1=int(input("Enter mid term marks of the first subject: "))
        mid_sub2=int(input("Enter mid term marks of the second subject: "))
        mid_sub3=int(input("Enter mid term marks of the third subject: "))
        mid_sub4=int(input("Enter mid term marks of the fourth subject: "))
        mid_sub5=int(input("Enter mid term marks of the fifth subject: "))
        midtermScore = (mid_sub1+mid_sub2+mid_sub3+mid_sub4+mid_sub5)/5
        return 'midtermScore'
    
    def final():
        fnl_sub1=int(input("Enter final marks of the first subject: "))
        fnl_sub2=int(input("Enter final marks of the second subject: "))
        fnl_sub3=int(input("Enter final marks of the third subject: "))
        fnl_sub4=int(input("Enter final marks of the fourth subject: "))
        fnl_sub5=int(input("Enter final marks of the fifth subject: "))
        finalScore = (fnl_sub1+fnl_sub2+fnl_sub3+fnl_sub4+fnl_sub5)/5
        return 'finalScore'
    
    total = 0
    highest = 0
    numStudents = int (input("How Many Students are there? "))
    while numStudents < 0 or numStudents > 100:
        numStudents = int (input("Please enter a number between 0 and 100? "))
    
    for i in range (numStudents):
        student_name = (input("Enter Student's Name Please: "))
        firstterm = firstTerm()
        midterm = midTerm()
        final = final()
        student_avg = ((firstterm) +str (midterm) +str (final)) / 3 
        if (highest < student_avg):
            highest = student_avg
            grade = student_avg  
            winner = student_name
            list_marks = []
            list_marks.append([student_name, student_avg,])
            print ("exam result: ", list_marks, "grade is: ", determine_score(grade))
    
    
    print ("The Student with the higgest average is: ", winner, "With the highest average of: ", highest, "gpa is: " + determine_score(grade) )
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Luc    6 年前

    在最终打印语句中,您需要确保所有内容都是字符串:

    print ("The Student with the higgest average is: ", winner, "With the highest average of: ", str(highest), "gpa is: " + determine_score(grade) )
    

    此外,函数应返回值,而不是硬编码字符串:

    return firsttermScore
    ...
    return midtermScore
    ...
    return finalScore
    

    如果您想获得精确的分数,还需要对double(或float)进行操作:

    firsttermScore = (fst_sub1+fst_sub2+fst_sub3+fst_sub4+fst_sub5)/5.0
    
    finalScore = (fnl_sub1+fnl_sub2+fnl_sub3+fnl_sub4+fnl_sub5)/5.0
    
    midtermScore = (mid_sub1+mid_sub2+mid_sub3+mid_sub4+mid_sub5)/5.0
    
    student_avg = (firstterm + midterm + final) / 3.0
    

    如果除以整数,则输入数据为整数。结果将为整数,您将失去精度。

    最后一条打印语句将失败,因为list\u marks是一个列表而不是字符串:

    print ("exam result: ", str(list_marks), "grade is: ", determine_score(grade))
    

    还有最后一个错误:

    finalGrade = final()
    

    您需要将变量的名称更改为与函数不同的名称。