Python Forum
passing a value to function problems
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing a value to function problems
#1
def main():

    customer_age = customer_age_selection() 

    insurance_response = input_insurance_response()

    insurance_weekly_coverage = insurance_process (insurance_response,customer_age)

    print_output(insurance_weekly_coverage,insurance_daily_coverage)

# get customer age
def customer_age_selection():    
    selection_is_invalid = True
    while selection_is_invalid:        
       print()
       customer_age = int(input("Enter your age: "))
       print()
       
       if customer_age < 18 or customer_age > 95:
            print ("age number inputed is invalid; re-enter your age: ")
       else:
            selection_is_invalid = False
            print()
            print ("Request is invalid please, re-enter your requested age")

    return customer_age



# get insurance response
def input_insurance_response():
    print()
    print ("The following are the insurance rates:")
    print("Age Of Driver     Daily Coverage     Weekly Coverage")
    print("18 to 25                $7                 $40      ")
    print("26 to 40                $5                 $30      ")
    print("Over 40                 $3                 $15      ")
    print()
    
    selection_is_invalid = True
    while selection_is_invalid:
        insurance_response = input("Do you want insurance?: ")

        if insurance_response == "yes":
            insurance_response = True
            selection_is_invalid = False

        elif insurance_response == "no":
            insurance_response = False
            selection_is_invalid = False

        else:
            selection_is_invalid = True
            print ("Please enter yes or no: ")
            print()

        return insurance_response


def insurance_process(insurance_response,customer_age):
    if insurance_response == "yes" and customer_age < 20 or customer_age > 25:
        insurance_daily_coverage = 7
        insurance_weekly_coverage = 40

    elif insurance_response == "yes" and customer_age < 26 or customer_age > 40:
        insurance_daily_coverage = 5
        insurance_weekly_coverage = 30

    return insurance_weekly_coverage,insurance_daily_coverage


def print_output(insurance_weekly_coverage):


    print("",insurance_weekly_coverage,"")


main()
the program is simple it takes input as age returns to main and yes or no response and returns to main, im calling the insurance_process function
and getting an error:UnboundLocalError: local variable 'insurance_weekly_coverage' referenced before assignment

is insurance_weekly_coverage = insurance_process (insurance_response,customer_age) an asssigment?
Reply
#2
Please post full error traceback message, in error tags, so we can see exact output and lines where it happens.

Yes, what you posted at the end of your post is an assignment. Result of insurance_process() function call is assigned to insurance_weekly_coverage.
Reply
#3
def myfunc(printthis):
    print(printthis)

myfunc('How about that')
# or:
zz = 'My name is zz'
myfunc(zz)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing a function to another function yulfmd1802 3 2,280 Jan-23-2020, 09:13 AM
Last Post: ndc85430
  Problems with isdigit function Omegax77 2 3,066 Jun-30-2018, 06:48 PM
Last Post: woooee
  There are problems with my data visualization function. Tony 1 2,304 May-20-2018, 10:09 AM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020