Search code examples
pythonglobal-variablesfunction-calls

call a function without leaving out input global variable


CHANGE_TOTAL = (float(input('type in the change total ')))
def main():
    value_1 = (float(input('type in the first value ')))
    value_2 = (float(input('type in the second value ')))
    value_3 = (float(input('type the third value ')))
    values_average(value_1,value_2,value_3)
def values_average(a,b,c):
    total = (a+b+c)
    if total >  CHANGE_TOTAL:
        print ('there\'s not change availibility at the moment plase wait')
        main()
    else:
        print ('your change is being process plase wait a moment')
    change_due(total)
def change_due(items_cost):
    input ('"press enter"')
    money_received = (float(input('type in the amount of money received ')))
    change = (money_received - items_cost)
    print ('your change is', change)
    change_total_aft_trans(change)
def change_total_aft_trans(a):
    change_left = (CHANGE_TOTAL - a)
    print ('the change left is', change_left)

main()

this is the thing as you can see in the ¨values_average funtion¨ when i try to loop the whole thing over by calling the ¨main function¨after the if-statement it doesn't asks me to type in a new CHANGE_TOTAL value, which is what i would like to do. any advice ? thanks in advance


Solution

  • I think relying on mutable global state is uncool. But you can easily do it that way, if you really want. Move your CHANGE_TOTAL = (float(input('type in the change total '))) inside main and put a global directive:

    def main():
        global CHANGE_TOTAL
        CHANGE_TOTAL = (float(input('type in the change total ')))
        value_1 = (float(input('type in the first value ')))
        value_2 = (float(input('type in the second value ')))
        value_3 = (float(input('type the third value ')))
        values_average(value_1,value_2,value_3)
    def values_average(a,b,c):
        total = (a+b+c)
        if total >  CHANGE_TOTAL:
            print ('there\'s not change availibility at the moment plase wait')
            main()
        else:
            print ('your change is being process plase wait a moment')
        change_due(total)
    def change_due(items_cost):
        input ('"press enter"')
        money_received = (float(input('type in the amount of money received ')))
        change = (money_received - items_cost)
        print ('your change is', change)
        change_total_aft_trans(change)
    def change_total_aft_trans(a):
        change_left = (CHANGE_TOTAL - a)
        print ('the change left is', change_left)
    

    Here would be a more sensible design, in my opinion:

    def main():
        change_total, *values = prompt_values()
        cost = sum(values)
        while cost > change_total:
            print("there's not change availibility at the moment plase wait")
            change_total, *values = prompt_values()
            cost = sum(values)
        change_due(cost, change_total)
    
    def prompt_values():
        change_total = float(input('type in the change total '))
        value_1 = float(input('type in the first value '))
        value_2 = float(input('type in the second value '))
        value_3 = (float(input('type the third value ')))
        return change_total, value_1, value_2, value_3
    
    def change_due(items_cost, change_total):
        print ('your change is being process plase wait a moment')
        input ('"press enter"')
        money_received = (float(input('type in the amount of money received ')))
        change = (money_received - items_cost)
        print ('your change is', change)
        change_total_aft_trans(change, change_total)
    
    def change_total_aft_trans(change, change_total):
        change_left = change_total - change
        print ('the change left is', change_left)
    

    Note, I use a while-loop instead of mutual recursion.