-2

I tried to execute the following code,

def dating_age (my_age):
    if my_age < 18 & my_age >= 13:
        girls_age = my_age/2+5
    elif my_age <13:
        print ("You are ineligible to date")
    else:
        girls_age = my_age/2+9
    return girls_age

my_age_input = int(input("Enter your age: "))
dating_limit = (dating_age(my_age_input))

if my_age_input < 13:
    print (dating_limit)
else:
    print ("I can date Girls of" , int(dating_limit) , "and higher")

and got the following error:

Enter your age: 12
You are ineligible to date
Traceback (most recent call last):
  File "C:/Users/animi/PycharmProjects/LearnPython/Functions.py", line 27, in <module>
    dating_limit = (dating_age(my_age_input))
  File "C:/Users/animi/PycharmProjects/LearnPython/Functions.py", line 24, in dating_age
    return girls_age
UnboundLocalError: local variable 'girls_age' referenced before assignment

Process finished with exit code 1

If i enter anything above 13 as input, there is no error code generated. Why is such an error occurring and how to avoid it?

Also, any tips to improve my programming practice is welcome.

Animikh Aich
  • 598
  • 1
  • 6
  • 15

3 Answers3

0

& is not the boolean operator, use and instead. The & operator is a bitwise operator and has a different precedence from the and operator.

As a result, your test is interpreted like this:

my_age < (18 & my_age) >= 13

which is a very different test indeed.

Use

my_age < 18 and my_age >= 13

or use a chained comparison:

13 <= my_age < 18

Next, when the age is lower than 13, you don't set girls_age to anything. You only print, so there is nothing set to that local and you get your error. Either raise the error or return a dummy value:

def dating_age (my_age):
    if 13 <= my_age < 18:
        girls_age = my_age / 2 + 5
    elif my_age < 13:
        print ("You are ineligible to date")
        return
    else:
        girls_age = my_age / 2 + 9
    return girls_age

You may as well return the values directly, and exit early when my_age is too low; in this example I'm using an exception and used a conditional expression to simplify the calculation:

def dating_age (my_age):
    if my_age < 13:
        raise ValueError("You are ineligible to date")
    return my_age / 2 + (5 if my_age < 18 else 9)

my_age_input = int(input("Enter your age: "))
try:
    dating_limit = dating_age(my_age_input)
except ValueError as ex:
    print(ex.args[0])
else:
    print("I can date girls of {} and higher".format(dating_limit))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Your problem is that you ALWAYS return the girls_age variable, while you define it only in two cases. Since it is not intentioned to define it in the second case (where you are below 13y), you could just return the age, wherever you have one:

def dating_age (my_age):
    if my_age < 18 AND my_age >= 13:
        return (my_age/2+5)
    elif my_age <13:
        print ("You are ineligible to date")
    else:
        return (my_age/2+9)
  • Another hint: You are defining a function, which should have only ONE function... sound weird.... however: Your function checks if you are able to date, which age is appropriate for dating and prints stuff to the shell..... – Mihail Gershkovich Mar 12 '17 at 17:47
0

Instead of another programming languages, variables are not automatically initialised in Python. You must initialised them in your code.

So when the interpreter reaches the return statement and my_age < 13, girls_age does not really exists and Python don't initialise it automatically. So it complains about no existing variable: girls_age is not assigned. You should set it before return.

QB_
  • 109
  • 7