Home > database >  unsupported operand type(s) for *: 'function' and 'int'
unsupported operand type(s) for *: 'function' and 'int'

Time:09-25

def main():
    midterm=midtermgra
    final=finalgra
    semgra=semesterGrade(midterm,final)
    category(semgra)
def midtermgra():
    midterm=eval(input('Please enter your midterm grade: '))
    return midterm
def finalgra():
    final=eval(input('Please enter your final grade: '))
    return final
def semesterGrade(midterm,final):
    semgra=(midterm (final*2))/3
    return semgra
def category(semgra):
    if(semgra<60):
        print('Semester grade: F ')
    elif(semgra<70):
        print('Semester grade: D ')
    elif(semgra<80):
        print('Semester grade: C ')
    elif(semgra<90):
        print('Semester grade: B ')
    else:
        print('Semester grade: A ')
main()

I'm trying to coding a structured program following teaching materials. However, it turns out to be a mistake 'TypeError: unsupported operand type(s) for *: 'function' and 'int''. I have reread my teaching slides, but can't figure out the problem. Could anybody help me? Thanks a lot!

CodePudding user response:

YOu wrote midterm = midtermgra and midtermgra is a function so that is the reason

CodePudding user response:

You want to execute functions in main() in the two lines below, but left out the parentheses so that the interpreter knows you want to call these functions instead of assigning the function to a variable. When you later use midtermgra to calculate, the value is assigned to a function name, but the interpreter is expecting an int, thus the error message.

midterm=midtermgra
final=finalgra

should be

midterm=midtermgra()
final=finalgra()
  • Related