Created
March 25, 2019 21:06
-
-
Save timbledum/daea05aa7e1b4ba2b0dd92163f0a9416 to your computer and use it in GitHub Desktop.
Helping out the fam here: https://www.reddit.com/r/learnpython/comments/b5fjix/having_trouble_working_with_classes/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SIZES = {"s": "Small", "m": "Medium", "l": "Large"} | |
PRICES = {"s": 1.5, "m": 1.75, "l": 2.0} | |
SWEETENED = {"s": "Sweetened", "u": "Unsweetened"} | |
class Beverage: | |
name = "Beverage" | |
def __init__(self, size, price): | |
self.size = size | |
self.price = price | |
def __str__(self): | |
return self.size + " " + str(self.price) + " " + self.name | |
class Soda(Beverage): | |
name = "Soda" | |
def __init__(self, size, price): | |
super().__init__(size, price) | |
class Lemonade(Beverage): | |
name = "Lemonade" | |
def __init__(self, size, price, sugar): | |
super().__init__(size, price) | |
self.sugar = sugar | |
def __str__(self): | |
return self.sugar + " " + super().__str__() | |
def title(): | |
print("The Beverage Program") | |
print("\n") | |
def closing(count, drinks=None): | |
print( | |
"You created " + str(count) + " drink orders. Thank you for using this program!" | |
) | |
if drinks: | |
print("The drinks you ordered are:") | |
for drink in drinks: | |
print(" ", drink) | |
def get_price(size_input, price): | |
drink_price = PRICES[size_input] | |
price = price + drink_price | |
return price | |
def main(): | |
drinks = [] | |
repeat = "y" | |
count = 0 | |
price = 0 | |
title() | |
while repeat == "y": | |
drinkType = input( | |
"Enter the kind of drink you'd like (S for soda, L for Lemonade, 0 to quit): \t" | |
) | |
drinkType = drinkType.lower() | |
if drinkType == "0": | |
repeat = input("would you like to make another drink? (y/n) \t") | |
if repeat == "y": | |
continue | |
elif repeat != "y": | |
closing(count) | |
return | |
while True: | |
if drinkType not in "sl0": | |
print("Invalid Entry. Please restart the program") | |
return | |
else: | |
break | |
size_input = input("What size would you like? Enter S, M or L:\t").lower() | |
while True: | |
if size_input not in SIZES: | |
print("Invalid Entry. Please restart the program") | |
return | |
else: | |
break | |
size = SIZES[size_input] | |
price = get_price(size_input, price) | |
if drinkType == "s": | |
drink = Soda(size, price) | |
if drinkType == "l": | |
sugar_input = input( | |
"Would you like it sweetened or unsweetened (s/u)? \t" | |
).lower() | |
sweetened = SWEETENED[sugar_input] | |
drink = Lemonade(size, price, sweetened) | |
drinks.append(drink) | |
if drinkType != "0": | |
print(drink, "ordered!") | |
repeat = input("Would you like to make another drink? (y/n) \t") | |
count += 1 | |
if repeat != "y": | |
closing(count, drinks) | |
return | |
else: | |
continue | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment