【发布时间】:2020-05-10 20:01:31
【问题描述】:
如果输入不等于整数,我将遍历用户的输入。我正在使用try: except value error,我收到一条错误消息:
UnboundLocalError: local variable 'averageHeartRate' referenced before assignment
我认为这意味着我的循环不起作用,它正在进入 if 语句的下一阶段,我该如何解决这个问题,我尝试添加其他 while True: 循环但它们不起作用。
def heartRate():
while True:
multiple_zone_question = input('Did you work in multiple heart zones? Answer Y for "Yes" or N for "No": ')
" i haven't finished writing this if statement"
if multiple_zone_question.lower() == 'y':
print('good')
break
" if user stayed only in one heart rate zone during exercise "
elif multiple_zone_question.lower() == 'n':
try:
"ask user for average heart rate "
avarageHeartRate = int(input('What was your Avarage Heart Rate during your exceresice? '))
except ValueError:
print("That's not an int!")
" average heart are should be no more then 3 numbers exm: 155 bpm"
if avarageHeartRate.len() > 3:
print('heart rate should be less then 200::\n Please enter a valid number')
'''else avarageHeartRate witch is an integer, multiplied by thresh
hold witch is also an integer stored outside of function (i probably
should move it inside the function right?). this will give me a
percentage (float) witch i can then store it into a SQL table as a
float int'''
else:
heartZone = avarageHeartRate /threshhold
return heartZone
"if multiple_zone_question is not y or n "
elif multiple_zone_question.lower() != 'y' or 'n' :
print("Invalid entry: please enter Y or N")
【问题讨论】:
-
在您的异常处理程序中,您有
if avarageHeartRate.len() > 3。但是如果遇到该异常,则不会设置avarageHeartRate。顺便说一句,根据您的代码,您希望avarageHeartRate是一个整数值,但整数没有len方法。