Reputation: 17
I am trying to make a bmi calculator but I am getting a NameError issue even though I have defined the bmi calculator. My code is like this:-
name1 = 'Person1'
height1_m = 1.7
weight1_kg = 54
name2 = 'Person2'
height2_m = 2
weight2_kg = 70
name3 = 'Person3'
height3_m = 1
weight3_kg = 1000
def bmi_calculator(name, height_m, weight_kg):
bmi = weight_kg / (height_m ** 2)
print(name + "'s bmi is: " + bmi)
if bmi > 25
print(name + 'is overweight')
else
print(name + 'is not overweight')
result1 = bmi_calculator(name1, height1_m, weight1_kg)
result2 = bmi_calculator(name2, height2_m, weight2_kg)
result3 = bmi_calculator(name3, height3_m, weight3_kg)
NameError Traceback (most recent call last)
<ipython-input-10-31aab3e09e71> in <module>
----> 1 result1 = bmi_calculator(name1, height1_m, weight1_kg)
2 result2 = bmi_calculator(name2, height2_m, weight2_kg)
3 result3 = bmi_calculator(name3, height3_m, weight3_kg)
NameError: name 'bmi_calculator' is not defined
Upvotes: 0
Views: 476
Reputation: 1097
1st Problem was, colon (:) at the end of if and else is required.
2nd problem was, you were trying to convert float to str implicitly.
This would work:
name1 = 'Person1'
height1_m = 1.7
weight1_kg = 54
name2 = 'Person2'
height2_m = 2
weight2_kg = 70
name3 = 'Person3'
height3_m = 1
weight3_kg = 1000
def bmi_calculator(name, height_m, weight_kg):
bmi = weight_kg / (height_m ** 2)
print(str(name) + "'s bmi is: " + str(bmi))
if bmi > 25:
print(name + 'is overweight')
else:
print(name + 'is not overweight')
result1 = bmi_calculator(name1, height1_m, weight1_kg)
result2 = bmi_calculator(name2, height2_m, weight2_kg)
result3 = bmi_calculator(name3, height3_m, weight3_kg)
Upvotes: 1