print("Welcome to the BMI Index Calculator.")
student_name = " "
while student_name != "0":
student_name = input("Please begin by entering the student's name, or 0 to quit:")
if student_name == "0":
print("Exiting program...")
exit()
def student_height():
input("Please enter student's height in inches:")
return
def student_weight():
input("Please enter student's weight in pounds:")
return
def bmi_profile():
print(student_name, "'s BMI profile:")
print("Height:", student_height, '"')
print("Weight:", student_weight, "lbs.")
def bmi_index(bmi):
bmi = (student_weight * 703 / student_height ** 2)
print("BMI Index:", bmi)
return bmi
循环运行但def函数未执行。谁能告诉我我的错误在哪里?我尝试多次修改缩进,但显然不是错误...... 提前感谢您的帮助。
答案 0 :(得分:3)
您必须先定义函数,然后在while循环中调用它们,如下所示:
修改
我做了一个编辑纠正了你所有的错误,我会一步一步解释:
首先,没有必要创建所有这些方法,如果你创建它们以便练习,那就没关系,但是如果你不返回数据,它就会在苹果酒空间:D:
def student_height():
return float(input("Please enter student's height in inches:")) #here you return the data
def student_weight():
return float(input("Please enter student's weight in pounds:")) #float is used to say the type of the input, because it came as a string
def bmi_profile(name, height, weight):
print(name, "'s BMI profile:")
print("Height:", height, '"')
print("Weight:", weight, "lbs.")
def bmi_index(height, weight):
bmi = float(weight * 703 / height ** 2)
print("BMI Index: " + str(bmi))
return bmi
print("Welcome to the BMI Index Calculator.")
student_name = " "
while student_name != "0":
student_name = input("Please begin by entering the student's name, or 0 to quit:")
if student_name == "0":
print("Exiting program...")
break
height = student_height() #here you store the data in a variable
weight = student_weight()
bmi_profile(student_name, height, weight) #here you use the data stored before
bmi_index(height, weight) #here also
答案较短:
这只有两个感觉......让它有点可读
def bmi_profile(name, height, weight):
print(name, "'s BMI profile:")
print("Height:", height, '"')
print("Weight:", weight, "lbs.")
def bmi_index(height, weight):
bmi = float(weight * 703 / height ** 2)
print("BMI Index: " + str(bmi))
print("Welcome to the BMI Index Calculator.")
student_name = " "
while student_name != "0":
student_name = input("Please begin by entering the student's name, or 0 to quit:")
if student_name == "0":
print("Exiting program...")
break
height = float(input("Please enter student's height in inches:"))
weight = float(input("Please enter student's weight in pounds:"))
bmi_profile(student_name, height, weight)
bmi_index(height, weight)