Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Coffee:
- # This class assumes you drink a full cup of coffee in 2 gulps
- gulp_phases = {
- "full": 0,
- "half": 1,
- "empty": 2
- }
- def __init__(self, coffee_mug: str):
- """
- param: coffee_mug: String to declare the fullness of the coffee. Allowed values are:
- - full
- - half
- - empty
- """
- if coffee_mug not in self.gulp_phases:
- print("Invalid coffee mug state. Defaulting to full.")
- self.coffee_mug = "full"
- self.gulps = 0
- else: # If "coffee_mug" is valid
- self.coffee_mug = coffee_mug
- self.gulps = self.gulp_phases[self.coffee_mug]
- def get_coffee_state(self):
- return self.coffee_mug
- def set_coffee_state(self, current_gulps: int):
- for mug, gulp in self.gulp_phases.items():
- if gulp == current_gulps:
- return mug
- return "empty" # If nothing is found. But it might never happen
- def refill_coffee(self):
- self.coffee_mug = "full"
- self.gulps = 0
- def drink(self):
- # Check the state of the coffee (i.e., full, empty)
- state = self.get_coffee_state()
- if state != "empty":
- self.gulps = self.gulps + 1
- self.coffee_mug = self.set_coffee_state(self.gulps)
- else: # If coffee_mug is empty
- self.refill_coffee()
- if __name__ == "__main__":
- my_coffee = Coffee(coffee_mug="full")
- while True:
- print(f"Coffee is {my_coffee.get_coffee_state()} ({my_coffee.gulps} gulps)")
- my_coffee.drink()
- # I wrote this piece of code because I saw a meme online on Facebook. A coffee mug with code on it to infinatly drink coffee. But that piece of code is broken. It's bad. Meme link: https://www.facebook.com/groups/182314357252077/permalink/1213079200842249/
Advertisement
Add Comment
Please, Sign In to add comment