I need to implement some kind of coffee machine. It has initial state of ingridients (WATER = 400; MILK = 540; BEANS = 120; EMPTY_CUPS = 9; MONEY = 550) and can do some actions (BUY = "buy" - buy some coffee(1 - espresso, 2 - latte, 3 - cappuccino); FILL = "fill" - add ingridients; TAKE = "take" - take all earned money; REMAINING = "remaining" - show remaining ingridients; EXIT = "exit"). After buying or filling the amount of ingridients changes. Here is my code
# actions
BUY = "buy"
FILL = "fill"
TAKE = "take"
REMAINING = "remaining"
EXIT = "exit"
# initial supply
WATER = 400
MILK = 540
BEANS = 120
EMPTY_CUPS = 9
MONEY = 550
# coffee
ESPRESSO = "1"
LATTE = "2"
CAPPUCCINO = "3"
water = WATER
milk = MILK
beans = BEANS
cups = EMPTY_CUPS
money = MONEY
def remaining():
print(f''' The coffee machine has:
{water} of water
{milk} of milk
{beans} of coffee beans
{cups} of disposable cups
{money} of money
''')
def fill():
global water, milk, beans, empty_cups, money
print('Write how many ml of water do you want to add:')
add_water = int(input())
print("Write how many ml of milk do you want to add:")
add_milk = int(input())
print("Write how many grams of coffee beans do you want to add:")
add_coffee = int(input())
print("Write how many disposable cups of coffee do you want to add:")
add_cups = int(input())
water = WATER + add_water
milk = MILK + add_milk
beans = BEANS + add_coffee
empty_cups = EMPTY_CUPS + add_cups
money = MONEY
return water, milk, beans, empty_cups, money
def take(money):
print(f"I gave you ${money}")
water = WATER
milk = MILK
beans = BEANS
empty_cups = EMPTY_CUPS
money = 0
return water, milk, beans, empty_cups, money
def buy():
global water, milk, beans, cups, money
print('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:')
coffee = input()
if coffee == ESPRESSO:
water = WATER - 250
milk = MILK
beans = BEANS - 16
cups = EMPTY_CUPS - 1
money = MONEY + 4
elif coffee == LATTE:
water = WATER - 350
milk = MILK - 75
beans = BEANS - 20
cups = EMPTY_CUPS - 1
money = MONEY + 7
else:
water = WATER - 200
milk = MILK - 100
beans = BEANS - 12
cups = EMPTY_CUPS - 1
money = MONEY + 6
return water, milk, beans, cups, money
def main():
remaining()
while True:
action = input("Write action (buy, fill, take, remaining, exit):")
if action == BUY:
buy()
elif action == FILL:
water, milk, beans, cups, money = fill()
elif action == TAKE:
water, milk, beans, cups, money = take(MONEY)
elif action == REMAINING:
remaining()
else:
break
main()
The problem is that the amount of ingridients changes only once. If I call "buy" or "fill" several times, the amount of ingridients changes only once.
Output:
The coffee machine has:
400 of water
540 of milk
120 of coffee beans
9 of disposable cups
550 of money
Write action (buy, fill, take, remaining, exit):buy
What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:
1
Write action (buy, fill, take, remaining, exit):remaining
The coffee machine has:
150 of water
540 of milk
104 of coffee beans
8 of disposable cups
554 of money
Write action (buy, fill, take, remaining, exit):buy
What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:
1
Write action (buy, fill, take, remaining, exit):remaining
The coffee machine has:
150 of water
540 of milk
104 of coffee beans
8 of disposable cups
554 of money
Write action (buy, fill, take, remaining, exit):
I'm new to Python and stuck totally. Could you tell me, please, how can I fix it? I need the amount of ingridients changes after every call of buy/fill.
Modified the code, maybe this is what you wanted:
# actions
BUY = "buy"
FILL = "fill"
TAKE = "take"
REMAINING = "remaining"
EXIT = "exit"
# initial supply
WATER = 400
MILK = 540
BEANS = 120
EMPTY_CUPS = 9
MONEY = 550
# coffee
ESPRESSO = "1"
LATTE = "2"
CAPPUCCINO = "3"
water = WATER
milk = MILK
beans = BEANS
empty_cups = EMPTY_CUPS
money = MONEY
def remaining():
global water, milk, beans, empty_cups, money
print(f''' The coffee machine has:
{water} of water
{milk} of milk
{beans} of coffee beans
{empty_cups} of disposable cups
{money} of money
''')
def fill():
global water, milk, beans, empty_cups, money
print('Write how many ml of water do you want to add:')
add_water = int(input())
print("Write how many ml of milk do you want to add:")
add_milk = int(input())
print("Write how many grams of coffee beans do you want to add:")
add_coffee = int(input())
print("Write how many disposable cups of coffee do you want to add:")
add_cups = int(input())
water+=add_water
milk+=add_milk
beans+=add_coffee
empty_cups+=add_cups
return water, milk, beans, empty_cups, money
def take(some_money):
global water, milk, beans, empty_cups, money
print(f"I gave you ${some_money}")
money-=some_money
return water, milk, beans, empty_cups, money
def buy():
global water, milk, beans, empty_cups, money
print('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:')
coffee = input()
if coffee == ESPRESSO:
water-=250
milk = milk
beans-=16
empty_cups-=1
money+=4
elif coffee == LATTE:
water-=350
milk-=75
beans-=20
empty_cups-=1
money-=7
else:
water-=200
milk-=100
beans-=12
empty_cups-=1
money+=6
return water, milk, beans, empty_cups, money
def main():
remaining()
while True:
action = input("Write action (buy, fill, take, remaining, exit):")
if action == BUY:
buy()
elif action == FILL:
water, milk, beans, cups, money = fill()
elif action == TAKE:
water, milk, beans, cups, money = take(MONEY)
elif action == REMAINING:
remaining()
else:
break
main()