Reputation: 17
I have written the following code, but I always get the same error message says (NameError: name 'weight_kg' is not defined) again and again even i change the code pls help(P.S I am a beginner):
First Try:
def profile():
name = input('Name: ')
weight_kg = int(input('Weight(Kg): '))
height_m = int(input('Height(M): '))
bmi_converter = (weight_kg / (height_m ** 2))
def bmi_result():
print(name)
if bmi < 25:
print('Is not overweight!')
else:
print('Overweight!')
profile()
bmi_result()
Second Try:
def profile():
name = input('Name: ')
weight_kg = int(input('Weight(Kg): '))
height_m = int(input('Height(M): '))
bmi_converter()
def bmi_converter():
bmi_formula = weight_kg / (height_m ** 2)
bmi_result()
def bmi_result():
print(name)
if bmi < 25:
print('Is not overweight!')
else:
print('Overweight!')
profile()
Upvotes: 1
Views: 575
Reputation: 17
I finally make my code works thanks to all of you I scramble some of your code into mine and make it shorter and simplier here it is:
name = input('Name: ')
weight_kg = int(input('Weight(Kg): '))
height_m = int(input('Height(M): '))
def bmi_calculator(name, weight_kg, height_m):
bmi = weight_kg / (height_m ** 2)
if bmi < 25:
print(name +' is not overweight!')
else:
print(name +' is overweight!')
result = bmi_calculator(name, weight_kg, height_m)
Upvotes: 0
Reputation: 2812
You can make this into a class, where each object stores the bmi profile for each person.
class bmi_profile:
def __init__(self, name, weight_kg, height_m):
self.name = name
self.weight_kg = weight_kg
self.height_m = height_m
self.bmi_result = weight_kg / (height_m ** 2)
def print_bmi(self):
print(self.name, end = ' ')
if self.bmi_result < 25:
print('Is not overweight!')
else:
print('Overweight!')
name = input('Name: ')
weight = float(input('Weight(Kg): '))
height = float(input('Height(M): '))
profile = bmi_profile(name, weight, height)
profile.print_bmi()
Output: (With input after the :
)
Name: Jon
Weight(Kg): 60
Height(M): 1.80
Jon Is not overweight!
Upvotes: 1
Reputation: 11
Your weight_kg, height_m and name variables scopes are in the profile function. So they cannot used in other functions. You can use these variables as parameters.
def profile():
name = input('Name: ')
weight_kg = int(input('Weight(Kg): '))
height_m = int(input('Height(M): '))
bmi_converter(name,weight_kg, height_m)
def bmi_converter(name,weight_kg, height_m):
bmi_formula = weight_kg / (height_m ** 2)
bmi_result(bmi_formula , name)
def bmi_result(bmi, name):
print(name)
if bmi < 25:
print('Is not overweight!')
else:
print('Overweight!')
profile()
Upvotes: 1
Reputation: 921
# My first try
def profile():
global weight_kg, height_m, name
name = input('Name: ')
weight_kg = int(input('Weight(Kg): '))
height_m = int(input('Height(M): '))
def bmi_result():
print(name)
if bmi_converter < 25:
print('Is not overweight!')
else:
print('Overweight!')
profile()
bmi_converter = (weight_kg / (height_m ** 2))
bmi_result()
Nice attempt Jon. You need to use global variables then it will work. Also, you forgot to refer the correct variable bmi_converter
I get the above output.
Upvotes: 1
Reputation: 3663
You can try this:
def profile():
name = input('Name: ')
weight_kg = int(input('Weight(Kg): '))
height_m = int(input('Height(M): '))
bmi_converter(weight_kg, height_m, name)
def bmi_converter(weight_kg, height_m, name):
bmi = weight_kg / (height_m ** 2)
bmi_result(bmi, name)
def bmi_result(bmi, name):
print(name)
if bmi < 25:
print('Is not overweight!')
else:
print('Overweight!')
profile()
Upvotes: 1