带异常处理python的BMI
我需要这段代码的帮助,我正在尝试应用,不久前我在索引中运行了一个bmi计算器,现在我正在尝试用异常处理更新这段代码。到目前为止,这些部分没有给我错误,它们只是奇怪地一起运行。例如,当它提示“输入用户名或“0”退出”时,它实际上并不结束进程,而是继续执行异常进程。有人能帮我更有效地写这篇文章吗。这是我更新的代码,我现在特别遇到的问题是,当用户输入“0”时,程序没有终止:带异常处理python的BMI,python,syntax,exception-handling,Python,Syntax,Exception Handling,我需要这段代码的帮助,我正在尝试应用,不久前我在索引中运行了一个bmi计算器,现在我正在尝试用异常处理更新这段代码。到目前为止,这些部分没有给我错误,它们只是奇怪地一起运行。例如,当它提示“输入用户名或“0”退出”时,它实际上并不结束进程,而是继续执行异常进程。有人能帮我更有效地写这篇文章吗。这是我更新的代码,我现在特别遇到的问题是,当用户输入“0”时,程序没有终止: def bmi_calculator(): end = False print("Welcome to the BMI Cal
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())
尝试使用
pass
而不是continue
,而end==False
后面没有缩进块,因此没有循环代码。您可能希望在def
s之后的主模块代码周围放置,而?我现在遇到的问题是,当有人输入“0”我将发布更新的代码时,代码没有终止