5 回答
TA贡献1780条经验 获得超1个赞
您可以将其放入一个类中,其中每个对象都存储每个人的体重数据。
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()
输出:(在 后输入:)
Name: Jon
Weight(Kg): 60
Height(M): 1.80
Jon Is not overweight!
TA贡献1871条经验 获得超13个赞
# 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()
不错的尝试,乔恩。您需要使用全局变量才能工作。另外,您忘记引用正确的变量bmi_converter
TA贡献2065条经验 获得超13个赞
你可以试试这个:
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()
TA贡献1825条经验 获得超4个赞
您的weight_kg、height_m 和name 变量范围位于profile 函数中。所以它们不能用于其他功能。您可以使用这些变量作为参数。
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()
TA贡献1796条经验 获得超4个赞
我终于让我的代码工作了,我将你们的一些代码打乱到我的代码中,并使其更短、更简单,如下:
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)
添加回答
举报