我需要尝试使用此代码,不久前,我在索引中运行了一个bmi计算器,现在我正在尝试使用异常处理来更新该代码。到目前为止,这些部分并没有给我带来错误,它们只是一起奇怪地运行。例如,当提示“输入用户名或'0'退出”时,它实际上并没有结束该过程,而是继续执行异常过程。有人可以帮助我更有效地编写此内容吗?这是我的代码,此代码已更新,我现在特别遇到的问题是,当用户输入“ 0”时程序没有终止:
def bmi_calculator():
end = False
print("Welcome to the BMI Calculator!")
while end == False:
user = input("Enter student's name or '0' to quit: ")
if user == "0":
print("end of report!")
end = True
else:
print("Lets gather your information,", user)
break
flag='yes'
while flag != 'no':
#Exception block for catching non integer inputs
try:
#Prompting user to input weight
weight = int(input('Enter your weight in pounds : '))
if weight == 0:
raise ValueError
except ValueError:
print ('Oops!! Kindly enter non-zero numbers only.')
flag = input('Do you want to try again? yes/no : ')
continue
try:
#Prompting user to input height
height = float(input('Enter your height in inches : '))
if height == 0:
raise ValueError
except ValueError:
print ('Oops!! Kindly enter non-zero numbers only.')
flag = input('Do you want to try again? yes/no : ')
continue
#Formula for calculating BMI
BMI = round(weight * 703/(height*height), 2)
return (BMI)
print(" Your bmi is:", bmi_calculator())