Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading