程序根据用户输入提供的重量和高度计算人的BMI。但是,在输入“metric”或“imperial”并按Enter后,程序将关闭。前三个打印功能正常工作,在我按下回车按钮后没有出现任何后续功能。我该如何解决这个问题?
print('\t\t\t BMI Calculator')
print('\t\t\t By Abdinasir Hussein')
print('\n Hello, this is a BMI Calculator!')
input('Do you wish to enter metric units or imperial units: ')
while input == 'metric':
height = float(input('Please enter your height input meters(decimals): '))
weight = int(input('Please enter your weight input kg: '))
bmi = weight/(height*height)
if bmi <= 18.5:
print('Your BMI is', bmi,'which means you are underweight.')
elif bmi > 18.5 and bmi < 25:
print('Your BMI is', bmi,'which means you are normal.')
elif bmi > 25 and bmi < 30:
print('your BMI is', bmi,'overweight.')
elif bmi > 30:
print('Your BMI is', bmi,'which means you are obese.')
else:
print('There is an error with your input')
print('Please check you have entered whole numbers\n'
'and decimals were asked.')
while input == 'imperial':
height = int(input('Please enter your height input inputches(whole number): '))
weight = int(input('Please enter your weight input pounds(whole number): '))
bmi = (weight*703)/(height*height)
if bmi <= 18.5:
print('Your BMI is', bmi,'which means you are underweight.')
elif bmi > 18.5 and bmi < 25:
print('Your BMI is', bmi,'which means you are normal.')
elif bmi > 25 and bmi < 30:
print('Your BMI is', bmi,'which means you are overweight')
elif bmi > 30:
print('Your BMI is', bmi,'which means you are obese.')
else:
print('There is an error with your input')
print('Please check you have entered whole numbers\n'
'and decimals were asked.')
input('\n\nPlease press enter to exit.')
我现在已经改变了,但是如何编辑这个块:
input = input('Do you wish to enter metric units or imperial units: ')
if input == 'metric':
height = float(input('Please enter your height input meters(decimals): '))
weight = int(input('Please enter your weight input kg: '))
bmi = weight/(height*height)
if bmi <= 18.5:
print('Your BMI is', bmi,'which means you are underweight.')
elif bmi > 18.5 and bmi < 25:
print('Your BMI is', bmi,'which means you are normal.')
elif bmi > 25 and bmi < 30:
print('your BMI is', bmi,'overweight.')
elif bmi > 30:
print('Your BMI is', bmi,'which means you are obese.')
else:
print('There is an error with you inputput')
print('Please check you have entered whole numbers\n'
'and decimals where asked.')
答案 0 :(得分:2)
您不应该使用while
。你打算写:
units = input('Do you wish to enter metric units or imperial units: ')
if units == 'metric':
......
elif units == 'imperial':
......
答案 1 :(得分:2)
而不是while input == 'imperial':
使用
else:
height = float(input('Please enter your height input inputches: '))
weight = int(input('Please enter your weight input pounds: '))
或使用
elif input == 'imperial':
height = float(input('Please enter your height input inputches: '))
weight = int(input('Please enter your weight input pounds: '))
else:
print("Please enter any one of the units")`
答案 2 :(得分:1)
输入是一个不可变的函数。在比较之前,您必须为变量分配输入。
in = input('Do you wish to enter metric units or imperial units: ')
然后
if (in == "Metric")
答案 3 :(得分:0)
你可以再次使用 就像
if units == 'metric':
height = ...
weight = ...
bmi = ...
if bmi <= 18.5:
print...
elif bmi > 18.5 and <= 25:
print...
.....
elif units == 'imperial':
height = ...
weight = ...
bmi = ...
if bmi <= 18.5:
print...
elif bmi..
....
else:
print('plese enter one units.')
并且不要忘记bmi = 25和30:)
答案 4 :(得分:0)
height = float(input("Your height in metres:"))
weight = int(input("Your weight in kilograms:"))
bmi = round(weight/ (height * height), 1)
if bmi <= 18.5:
print('Your BMI is', bmi, 'which means you are underweight.')
elif bmi > 18.5 and bmi < 25:
print('Your BMI is', bmi, 'which means you are normal.')
elif bmi > 25 and bmi < 30:
print('Your BMI is', bmi, 'which means you are overweight.')
elif bmi > 30:
print('Your BMI is', bmi, 'which means you are obese.')
else:
print('There is an error with your input')
答案 5 :(得分:0)
# this calculates Body Mass Index using Metrics standard
def bmi_metrics():
weight = float(input("enter your mass in kilogram (KG) : " ))
height = float(input("enter your height in metres(M) : " ))
bmi = weight * (height**2)
print("your body mass index = ".capitalize(), bmi, " kg/m2")
if bmi <= 18.5 :
print("you are below standard body mass, you should eat food rich in fats and eat regularly".capitalize())
bmi_calculator()
elif bmi >= 18.5 and bmi <= 24.9:
print("you have a normal standard weight".upper())
bmi_calculator()
elif bmi >= 25 and bmi <= 29.9:
print("you over weighed normal standard weight".upper())
bmi_calculator()
else:
print("you have obesity and needs quick medical attention".upper())
response = input("Do you wish to continue? type Y for (yes) or N for (no): ")
if response == "y" or response == "Y":
bmi_calculator()
elif response == "n" or response == "N":
print(" Thanks for using this program ")
exit()
# This calculates Body Mass Index using Imperial standard
def bmi_imperial():
weightInKilogram = float(input("enter your mass in kilogram (kg) : "))
heightInMeters = float(input("enter your height in metres(m) : " ))
weight = weightInKilogram * 2.2 # covert kg to pounds
height = heightInMeters / 0.0254 # convert meters to inches
bmi = (weight * 703)/(height**2)
print("your weight in pounds = ", weight, "lbs")
print("your height in inches = ", height, "inches")
print("your body mass index = ".capitalize() , bmi, "lbs/inches2")
if bmi <= 18.5 :
print("you are below standard body mass, you should eat food rich in fats and eat regularly".capitalize())
elif bmi >= 18.5 and bmi <= 24.9:
print("you have a normal standard weight".capitalize())
bmi_calculator()
elif bmi >= 25 and bmi <= 29.9:
print("you over weighed normal standard weight".capitalize())
bmi_calculator()
else:
print("you have obesity and needs quick medical attention".capitalize())
response = input("Do you wish to continue? type Y for (yes) or N for (no): ")
if response == "y" or response == "Y":
bmi_calculator()
elif response == "n" or response == "N":
print(" Thanks for using this program ")
exit()
# This is the main and it computes the BMI with either standard based on user response
def bmi_calculator():
print("""
******************************************************************
Welcome to Body Mass Index Calculator (BMI = M/H**2)
******************************************************************
""")
#Ask if user would like to use Metric or Imperial units
response = input("would you like to calculate your Body Mass Index in Metrics or Imperial, type Metrics or Imperial: ").lower()
if response == 'metrics':
print("---------you have chosen to compute your BMI in---------", response.upper())
bmi_metrics() # This calls and computes the metrics standard function
elif response == 'imperial':
print("---------you have chosen to compute your BMI in---------", response.upper())
return bmi_imperial()# This calls and computes the imperial standard function
else:
print("invalid response please try again".capitalize())
bmi_calculator()
bmi_calculator()