繁体   English   中英

尝试除值错误,未绑定局部变量

[英]try and except value error , unbound local variable

如果输入不等于 integer,我将遍历用户的输入。 我正在使用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")

处理averageHeartRate就好像它是 integer 的代码驻留在except块中,当输入值不是 integer 时调用该块,因此永远不会分配给averageHeartRate 您可能应该执行以下操作(注意else:语句):

            try:
                avarageHeartRate = int(input('What was your Avarage Heart Rate during your exceresice? '))
            except ValueError:
                print("That's not an int!")
            else:
                if avarageHeartRate.len() > 3:
                    print('heart rate should be less then 200::\n Please enter a valid number')
                # and so on...

而不是把它全部写在一个 function 中,我决定在主 function 中写 2 个 function。 如果答案是肯定的,一个将处理,另一个 function 将处理“否”。 有点乱,但更容易阅读

    ddef heartRate():
        'i moved the threshold var inside the function'
        threshold = int(183)
        multiple_zone_question = input('Did you work in multiple heart zones? Answer Y  for "Yes" or N for "No": ')

        if multiple_zone_question.lower() == 'y':
            def answerYES():
                pass

        elif multiple_zone_question.lower() == 'n':       
            def answerNO():
                while True:
                   'catch if answer is not a number'
                    try:
                        avaregeHeartRate = int(input('What was your Avarage Heart Rate during your exceresice? '))
                    except ValueError:
                        print('answer must be a number: Try Again')
                    'makes sure answer is less then 200. '
                    else:
                        if avaregeHeartRate > 200:
                            print('heart rate should be less then 200')
                        'if there is no problems we can then find the heart zone'
                        else:
                            heart_zone = avaregeHeartRate /threshold
                            'i added a round() to simplify the float number'
                            return round(heart_zone, 2)
                            break

        elif multiple_zone_question.lower() != 'y' or 'n' :
            print("Invalid entry: please enter Y or N")

我的大脑告诉我 lambda function,以简化。 一旦我弄清楚如何

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM