Home > Blockchain >  TypeError: unsupported operand type(s) for : 'int' and 'str', but actually a si
TypeError: unsupported operand type(s) for : 'int' and 'str', but actually a si

Time:12-18

#function vathmon
def grade_list(grade):
    test_num = int(input('How many test scores do you want to enter: '))
    for g in range(test_num):
        print('Enter grade for student #', g 1,sep='')
        grade.append(input('Enter score: '))
    return grade    
#functiopn mesou orou
def average_grade(grade, ):
    grade = int(sum(grade))/(len(grade))
    return grade

def show_results(grade, over):
    for i in range(grade):
        if grade > over :
                  print(grade)              
    

def main():
    grades = []
    grade_list(grades)
    final_result = average_grade(grades)
    show_results(final_result)

main()    

CodePudding user response:

Your grade_list function returns a list of strings, but average_grade expects a list of numbers (it calls sum(grade), which trades the contents of the list as numbers).

Here's a fixed version of the code with type annotations that show what type of parameters each function is expected to take. Note that grade_list has been modified to return a List[int], by making sure that each item appended to grades is an int. We can then just pass the resulting list to statistics.mean rather than having to implement our own averaging function.

from statistics import mean
from typing import List


def grade_list() -> List[int]:
    test_num = int(input('How many test scores do you want to enter: '))
    grades: List[int] = []
    for g in range(1, test_num   1):
        print(f'Enter grade for student #{g}')
        grades.append(int(input('Enter score: ')))
    return grades


def show_results(grades: List[int], over: float) -> None:
    for grade in grades:
        if grade > over:
            print(grade)


def main() -> None:
    grades = grade_list()
    average = mean(grades)
    show_results(grades, average)


if __name__ == '__main__':
    main()
How many test scores do you want to enter: 3
Enter grade for student #1
Enter score: 100
Enter grade for student #2
Enter score: 90
Enter grade for student #3
Enter score: 50
100
90

CodePudding user response:

try: print('Enter grade for student #', int(g) 1,sep='')

  • Related