-1

Hey guys im new here and im just starting out. I tried to make a BMI calculator by simply getting input and doing calculations but it doesn't work for some reason. This is the program:

print "how old are you?",
age = raw_input()
print "how tall are you?",
height = raw_input()
print "how much do you weigh?",
weight = raw_input()

print "So, you're %r years old, %r meters tall and %r Kilograms heavy.\n Ain't too bad but could be better to be honest!" %(age, height, weight)

print "Your BMI is %d" % (weight (height * height))

and this is the output:

how old are you? 1
how tall are you? 2
how much do you weigh? 4
So, you're '1' years old, '2' meters tall and '4' Kilograms heavy.
 Ain't too bad but could be better to be honest!
Traceback (most recent call last):
  File "ex10.py", line 11, in <module>
    print "Your BMI is %d" % (weight (height * height))
TypeError: can't multiply sequence by non-int of type 'str'

Thanks guys!!!

Will
  • 2,163
  • 1
  • 22
  • 22

1 Answers1

0

Your user input values are strings. You are trying to perform mathematical operations on them. You need to convert them to numbers first.

Here is a question that will set you on the path to converting the strings to integers: How to convert strings into integers in Python?

Looking closer I believe your issue is that this line is incorrect:

print "Your BMI is %d" % (weight (height * height))

You're attempting to call a method weight passing in 2 strings multiplied. This won't work for several reasons. I believe you want:

print "Your BMI is %d" % (weight / (height * height))

Notice the division operator in there. I looked up BMI and found the equation to be weight over height squared.

Community
  • 1
  • 1
jaydel
  • 14,389
  • 14
  • 62
  • 98
  • I tried many of these but failed. If I understand correctly I should turn the string into int in the form of: height = int (height). – Yossi Gershon Aug 16 '16 at 13:30
  • Have you tried: `print "Your BMI is %d" % (int(weight) (int(height) * int(height)))`? – jaydel Aug 16 '16 at 13:42
  • Yes, it says : TypeError: 'int' object is not callable – Yossi Gershon Aug 16 '16 at 15:46
  • 1
    I wrote your code locally and ran it. What are you attempting to do with `(weight (height * height))` ? it looks like you're calling a method named weight? Were you possibly looking for `weight / (height * height)`? – jaydel Aug 16 '16 at 17:03
  • Thanks jaydel! How stupid am I??? I also changed it to float instead of int so that it could take all numbers. – Yossi Gershon Aug 17 '16 at 04:33