TypeError: unsupported operand type(s) for /: 'str' and 'float' in Python

Dung Do Tien Aug 15 2021 67

Hello you guys, I have a problem need your help. I created a small project with Python 3.8.2 to help calculate BMI points for humans. You can see like this:

weight = input('How much do you weigh?:')
height = float(input('How tall are you?:'))
bmi = weight / (height *2)
print('BMI: ', bmi)

But when I'm inputting weight = 78 and tall = 1.75 I get an error throw TypeError: unsupported operand type(s) for /: 'str' and 'float'

How much do you weigh?:78
How tall are you?:1.75
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    bmi = weight / (height *2)
TypeError: unsupported operand type(s) for /: 'str' and 'float'

How can I resolve it?

Thanks for any suggestions.

Have 1 answer(s) found.
  • D

    Diego Ganchozo Aug 15 2021

    You need to convert the input string to int or float number by using int() or float() methods before you execute multiplication:

    weight = float(input('How much do you weigh?:'))
    height = float(input('How tall are you?:'))
    bmi = weight / (height * 2)
    print('BMI: ', bmi)

    And below is the output:

    How much do you weigh?:89
    How tall are you?:1.73
    BMI:  25.722543352601157

    hope it helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close