5 回答

TA貢獻1780條經(jīng)驗 獲得超1個贊
您可以將其放入一個類中,其中每個對象都存儲每個人的體重數(shù)據(jù)。
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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超14個贊
你可以試試這個:
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條經(jīng)驗 獲得超4個贊
您的weight_kg、height_m 和name 變量范圍位于profile 函數(shù)中。所以它們不能用于其他功能。您可以使用這些變量作為參數(shù)。
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條經(jīng)驗 獲得超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)
添加回答
舉報