Home > database >  Can't I make inter-referencing functions in Python?
Can't I make inter-referencing functions in Python?

Time:03-09

I'm taking an online-coure in Udemy

Doing my coffee-machine project, I made some functions like

def machine_input():
    coffee_needs = input("What would you like?, espresso, latte or cappuccino\n").lower()
    if coffee_needs == 'report':
        print(f"The current resource values \n Water:{resources['water']} ml \n milk:{resources['milk']} \n coffee:{resources['coffee']}")
        return False
        coffee_machine()
    else:
        return coffee_needs

and

def coffee_machine():
    while keep_working:
        coffee_needs = machine_input()
        check_resources(coffee_needs)
        total_money = get_coins()
        check_transaction(total_money, coffee_needs)
        dimming_resources(coffee_needs)
        if not check_transaction:
            coffee_machine()

but in this case, two functions didn't work as I expected.

It show me an error when I input 'report' in machine_input().

I'd like to restart the function 'coffee_machine()' in 'machine_input()'

But now I'm thinking that I may not be able to inter-refer two functions.

Like coffee_machine in machine_input in coffee_machine in machine_input in coffee_machine in machine_input in....

Is it possible to do like this in python?

(I'm doing this with 3.10.2 ver)

CodePudding user response:

It looks like you're doing unintended recursion. You're actually creating multiple separate coffee machines that spawn even more coffee machines. Unless it's supposed to be a weird coffee machine factory, it only seems that your code is doing what you think it is.

What I assume you mean by inter-referencing is just going back and forth between the two functions.

  1. You don't need to explicitly go back from machine_input() by calling coffee_machine() there. Just return something to finish the function and you'll be back wherever you called it from. Otherwise you're spawning a totally new coffee machine, inside the first one.
  2. You don't have to call coffee_machine() in your coffee_machine(). You're already in a while loop, so I'm guessing you wanted to continue by starting over on the same coffee machine. Otherwise you're creating a totally new coffee machine, again.
def machine_input():
    coffee_needs = input("What would you like?, espresso, latte or cappuccino\n").lower()
    if coffee_needs == 'off':
        print('Turning off th machine')
        return False
    elif coffee_needs == 'report':
        print(f"The current resource values \n Water:{resources['water']} ml \n milk:{resources['milk']} \n coffee:{resources['coffee']}")
        return True
    else:
        return coffee_needs

def coffee_machine():
    while keep_working:
        coffee_needs = machine_input()
        if not coffee_needs:
            print('No coffee needs specified.')
            continue

        check_resources(coffee_needs)
        if not check_resources:
            print('Not enough resources.')
            continue

        total_money = get_coins()
        check_transaction(total_money, coffee_needs)
        dimming_resources(coffee_needs)
        if not check_transaction:
            print('Insufficient funds.')
            continue

Consider tea.

CodePudding user response:

You are basically trying to build a state machine, but the bad way (by cross-referencing functions).

To keep this simple, you could use a while loop with a global variable for the state ("working", "input" and "stop" here for example), which you modify accordingly in each of the state (= each function) and use it to select which function to run next.

  • Related