Search code examples
python-3.x

TypeError for /: 'str' and 'int'


this is my code:

score1 = input("enter your math score:\n")
score2 = input("enter your science score:\n")
score3 = input("enter your physics score:\n")
score4 = input("enter your statistics score:\n")
score5 = input("enter your geometry score :\n")
average = (score1 + score2 + score3 + score4 + score5)//5

and i have got TypeError: unsupported operand type(s) for //: 'str' and 'int'


Solution

  • The default type of input field is string, so whenever you type the value that stores the number in string datatype. So you need to do type casting or add int() method.

    score1 = int(input("enter your math score:\n"))
    score2 = int(input("enter your science score:\n"))
    score3 = int(input("enter your physics score:\n"))
    score4 = int(input("enter your statistics score:\n"))
    score5 = int(input("enter your geometry score :\n"))
    
    average = (score1 + score2 + score3 + score4 + score5)//5