'How to update in Init an element that is updated in another function in the program?
How to update the main self.money value by take How any change in function in the program will cause a general change in the values of the parameters defined in Init And not just a local change of value in a function
For example in my code I choose "take" And then in "remaining" "remaining" need to print $ 0 of money And he prints $ 550 of money Anyone know what I need to add / change in my code for this to work?
class CoffeeMachine():
def __init__(self):
self.water = 400
self.milk = 540
self.coffee_beans = 120
self.disposable_cups = 9
self.money = 550
def take(self):
print(f"I gave you ${self.money}")
self.money = 0
def remaining(self):
print(f"""
The coffee machine has:
{self.water} of water
{self.milk} of milk
{self.coffee_beans} of coffee beans
{self.disposable_cups} of disposable cups
${self.money} of money
""")
def action(self):
while True:
print("Write action (take, remaining, exit):")
action = input()
if action.lower() == 'exit':
break
elif action.lower() == 'remaining':
CoffeeMachine().remaining()
continue
elif action.lower() == 'take':
CoffeeMachine().take()
CoffeeMachine().action()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
