简体   繁体   中英

try and except value error , unbound local variable

I'm looping through a user's input if the input is not equated to an integer. I'm using a try: except value error , I get an error message saying:

UnboundLocalError: local variable 'averageHeartRate' referenced before assignment

i think that means my looping is not working and it's going into the next stage of the if statement, how can i fix this, i have tried adding other while True: loops but they are not working.

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")

The code that processes averageHeartRate as if it was an integer resides inside the except block that is invoked when the input value is not an integer, and is, therefore, never assigned to averageHeartRate . You should probably do something like the following (note the else: statement):

            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...

instead of writing it all in one function i decided to write 2 function inside the main function. one will handle if the answer is yes and the other function will handle the 'no'. iits a little messy but its easier to read

    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")

my brain is telling me lambda function, to simplify. i will make it more pythonic once i figure out how to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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