簡體   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