如何为函数中的变量分配新值,Python?
How to assign a new value for a variable in function, Python?
我需要实现某种咖啡机。它具有初始状态的 ingridients (WATER = 400;
牛奶 = 540;豆子 = 120; EMPTY_CUPS = 9; MONEY = 550) 并且可以执行一些操作(BUY = "buy" - 买一些咖啡(1 - 浓缩咖啡,2 - 拿铁咖啡,3 - 卡布奇诺);FILL = "fill" - 添加成分;TAKE = "take" - 拿走所有挣钱;REMAINING =“剩余”- 显示剩余成分;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 的新手,完全卡住了。请你告诉我,我该如何解决?每次调用 buy/fill.
后,我需要改变成分的数量
每次您调用 water = WATER + add_water
和类似的方法时,您都在添加一个永不改变的常量(大写)变量。你想要的是常量中的 water += add_water
等等。
修改了代码,也许这就是你想要的:
# 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()
我需要实现某种咖啡机。它具有初始状态的 ingridients (WATER = 400; 牛奶 = 540;豆子 = 120; EMPTY_CUPS = 9; MONEY = 550) 并且可以执行一些操作(BUY = "buy" - 买一些咖啡(1 - 浓缩咖啡,2 - 拿铁咖啡,3 - 卡布奇诺);FILL = "fill" - 添加成分;TAKE = "take" - 拿走所有挣钱;REMAINING =“剩余”- 显示剩余成分;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 的新手,完全卡住了。请你告诉我,我该如何解决?每次调用 buy/fill.
后,我需要改变成分的数量每次您调用 water = WATER + add_water
和类似的方法时,您都在添加一个永不改变的常量(大写)变量。你想要的是常量中的 water += add_water
等等。
修改了代码,也许这就是你想要的:
# 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()