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)
>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.