0

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")
Takos
  • 69
  • 9
  • In your exception handler you have `if avarageHeartRate.len() > 3`. But `avarageHeartRate` is not set if that exception is reached. BTW, accorsing to your code you expect `avarageHeartRate` to be an integer value but an integer doesn't have a `len` method. – Matthias May 10 '20 at 20:06
  • 1
    Maybe you would like to read [Asking the user for input until they give a valid response](/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias May 10 '20 at 20:08

2 Answers2

0

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...
Amitai Irron
  • 1,973
  • 1
  • 12
  • 14
0

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