如何在函数Python中为变量分配新值



我需要实现某种咖啡机。它具有初始的成分状态(WATER=400;牛奶=540;BEANS=120;EMPTY_CUPS=9;MONEY=550(并且可以做一些动作(BUY="买"-买一些咖啡(1-意式浓缩咖啡,2-拿铁,3-卡布奇诺(;FILL=";填充"-添加配料;TAKE=";取"-拿走所有赚来的钱;剩余=";剩余"-显示剩余的成分;EXIT=";退出(。购买或填充后,成分的数量会发生变化。这是我的代码

# 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 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):

我是Python的新手,完全被卡住了。你能告诉我,我该怎么修吗?我需要每次调用购买/填充后的成分数量变化。

修改了代码,也许这就是您想要的:

# 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()

每次调用water = WATER + add_water和类似函数时,都会添加到一个永远不变的常量(大写(变量中。你想要的是常数中的water += add_water等等。

相关内容

  • 没有找到相关文章

最新更新