関数が何かを返す場合は、それをどこかに配置する必要があります。たとえば、変数で!
ここで、関数を変更します。
def main():
name = input("What is your first name?: ")
name2 = input("What is your last name?: ")
mass = float(input("What is your weight in kilograms?: "))
height = float(input("What is your height in meters?: "))
#mass = float(kg) #not needed
#height = float(meters) #not needed
result = health(mass, height)
#printing based on the return value. result[0] is bmi, and so on.
print("%s, %s (BMI: %d, Ponderal Index: %d, Rohrer's Index: %d"%(name2,name,health[0],health[1],health[2]))
def bmi (mass, height):
result = mass / (height ** 2)
return result
def ponderal (mass, height):
result = mass / height ** 3
return result
def rohrer(mass, height):
result = (mass * 10000) / ((height * 100) ** 3)
return result
def health (mass, height):
#calling the functions
bmi = bmi(mass, height) #store the returned value to a variable
ponderal = ponderal(mass, height)
rohrer = rohrer(mass, height)
return [bmi,ponderal,rohrer] #return it as a list.
結果:
>>> ================================ RESTART ================================
>>>
What is your first name?: Akhyar
What is your last name?: Kamili
What is your weight in kilograms?: 50
What is your height in meters?: 1.7
Kamili, Akhyar (BMI: 17.301038062283737 , Ponderal Index: 10.177081213108082 , Rohrer's Index: 0.1017708121310808 , )
>>>
いくつかのアドバイス:
- 関数名を大文字にしないでください!
- 関数のように変数に名前を付けないでください!
あなたのコードはより良くなります。