Reputation: 1
I'm using Python 3.5.1, and I need to make a BMI calculator using the formula 703 * weight/height^2, after I input my height and weight, I get "TypeError: can't multiply sequence by non-int of type 'str'"
And I'm honestly not sure how to fix it. This is my code.
def calculateBMI():
weight = input("Please enter weight in pounds: ")
height = input("Please enter height in inches: ")
return weight * ((703.0) / (height * height))
bmi = calculateBMI()
print ("""Your BMI is""", str(bmi))
if bmi < 18.5:
print("You are underweight.")
elif bmi > 25:
print("You are overweight.")
else:
print ("You are of optimal weight.")
Upvotes: 0
Views: 1954
Reputation: 376
Before I help out, I just wanted to point out that the code you have pasted has no indentation. Python is indentation-sensitive -- did you just paste it wrong, or is this how your code actually looks? :)
Now, there are probably two problems here:
When I try running this code, it is able to take input from the
command line fine. I am using Python 2.7.8. The raw_input
method
has been renamed to input
in Python 3. So, if you are using
Python 3, you should change the raw_input
to input
.
If you are on Linux, you can find out your Python version on the console like this:
$ python --version
When you take input from the command line, using input
or raw_input
, it is saved as a string, as shown here in the docs
https://docs.python.org/3/library/functions.html#input
If you want to multiply the two values together, you have to convert them to float, like this:
weight = float(input("Please enter weight: "))
height = float(input("Please enter height: "))
I hope this solves your problems :)
Upvotes: 0
Reputation: 168636
There are three errors in your program:
Since you are using Python3, you need to use input()
, not raw_input()
to read the user's weight and height.
You need to convert the user's data to a numeric type using int()
or float()
.
Your indentation is incorrect.
Here is a program that works:
def calculateBMI():
weight = int(input("Please enter weight: "))
height = int(input("Please enter height: "))
return weight * ((703.0) / (height * height))
bmi = calculateBMI()
print ("""Your BMI is""", str(bmi))
if bmi < 18.5:
print("You are underweight.")
elif bmi > 25:
print("You are overweight.")
else:
print ("You are of optimal weight.")
Upvotes: 4