This example is just a basic program - I'm a new Coder - learning and experimenting whilst messing about .. Currently testing on Python 3.6 IDE and PyCharm - apologies for double spacing code - but looks a mess without.
Looking for guidance for returning a value from a function.
Have tried dozens of different methods / searched the forum, but closest answer this layman could understand stated I needed to use the return value otherwise it will be forgotten .. So added print(age_verification, " example test value .. ") at various locations - but nothing gets returned outside the function ..
Have tried returning Boolean / integer / string values and adapting - nothing with each variant .. Added a default age_verification = False variable before the function // or // referenced within function for 1st time .. Doesn't effect the return value except IDE doesn't state "unresolved reference"
Tried a line-by-line python visualizer - but again - age_verification value disappears instantly after exiting the function . :-(
==================================================================
Using 1 Single Function
def age_veri(age, age_verification) :
if age < 18 :
age_verification = False
print(age_verification, " is false .. Printed to test variable ..")
return age_verification
elif age >= 18:
age_verification = True
print(age_verification, " is True.. Printed to test variable ..")
return age_verification
return age_verification # ( -- have tested with/without this single-indent line & with/without previous double-indent return age_verification line.)
age=int(input("Enter Your Age : ")
age_verification = False # ( -- have tried with / without this default value)
age_veri(age, False)
if age_verification is False:
print("You failed Verification - Age is Below 18 .. ")
elif age_verification is True:
print("Enter Website - Over 18yrs")
else:
print(" Account not Verified .. ")
==================================================================
Same Example - Using 2 Functions
def age_variable(age):
if age < 18:
age_verification = False
print (age_verification, " printing here to use value and help test function..")
return age_verification
elif age >= 18:
age_verification = True
print (age verification, " printing here to use value and help test function..")
return age_verification
return age_verification (tried with and without this line - single indent - same level as if / elif)
def are_verified(age_verification):
if age_verification is False:
print("Age Verification Failed .. ")
elif age_verification is True:
print("Visit Website .. ")
else:
print("Verification Incomplete .. ")
age = int(input("Enter Your Age : ")
age_variable(age)
are_verified(age_verification)
==============================================================
Any advice is appreciated - wasted most of today hitting my head against the wall .. And apologies in advance .. Know it'll be something really basic - but appear to be using same formatting as others :-)
THANK YOU