vendredi 12 mars 2021

How can I add object to a list and print the object using Class?

I am trying to create a program to get the beverage type and size using Class and Function. After collecting the beverage(either lemonade or soda), I want to assign all values to an object and eventually add the object to a list. Finally, I want to show something like the below console:

The Beverage Program

Soda or a lemonade? Enter S, L or 0 for no drinks: S

What size would you like? Enter S, M or L: M

Would you like to create another drink? y/n? y

Soda or a lemonade? Enter S, L or 0 for no drinks: L

What size would you like? Enter S, M or L: L

Sweetened or Unsweetened? Enter S or U: U

Would you like to create another drink? y/n? y

Soda or a lemonade? Enter S, L or 0 for no drinks: L

What size would you like? Enter S, M or L: S

Sweetened or Unsweetened? Enter S or U: S

Would you like to create another drink? y/n? n

Drink 1: Soda

Size: Medium Price: $1.75

Drink 2: Lemonade

Size: Large Price: $2.00 Sugar: Unsweetened

Drink 3: Lemonade

Size: Small Price: $1.50 Sugar: Sweetened

You created 3 drink orders. Thanks for using this program!

        print('The Beverage Program\n')

def closing(total):
        print('\nYou created', total,'drink orders. Thanks for using this program!')

class Beverage:
    def __init__(self,size,price):
        self.size = size
        self.price = price
        
    def display(self):
        print('Size:',self.size,"Price: $",self.price)
        
class Soda(Beverage):
    def __init__(self,size,price):
        Beverage.__init__(self,size,price)

    def display(self):    
        print('Size:',self.size,"Price: $",self.price)
        
class Lemonade(Soda):
    def __init__(self,size,price,sugar):
        Soda.__init__(self,size,price)
        self.sugar = sugar

    def display(self):    
        print('Size:',self.size,"Price: $",self.price,'Sugar: '+str(self.sugar))
def get_size(size_input):
        if size_input == 'l':size = 'Large'
        elif size_input == 'm':size = 'Medium'
        else: size = 'Small'
        return size

def get_price(size_input):
    if size_input == 'l': price=2.0
    elif size_input == 'm': price = 1.75
    else: price = 1.5
    return price

def get_sugar(drink):
    while drink == 'l':
        sugar = str.lower(input('Sweetened or Unsweetened?  Enter S or U:\t'))
        if sugar=='s':sugar_input = 'Sweetened'
        else:sugar_input = 'Unsweetened'
        return sugar_input

def get_drink_name(drink):
    for drink in drink:
        if drink == 's': drink_name='Soda'
        elif drink =='l': drink_name = "Lemonade"
        else: break
    return drink_name

def show_products(total,drinks):
        for drink in drinks:
            if isinstance(drink,Soda):
                    print('\nDrink',str(total)+': Soda')
            elif isinstance(drink,Lemonade):
                    print('\nDrink',str(total)+': Lemonade')
            drink.display()

def write_drink_list(drink,size,price,sugar):
    drinks=[]
    for drink in drink:
        if drink =='s':
            drinks.append(Soda(size,price))
        elif drink =='l':
            drinks.append(Lemonade(size,price,sugar))
    return drinks
                
def main():
    drinks=[]
    title()
    again = 'y'
    total = 0
    while again=='y':
        drink = str.lower(input('\nSoda or a lemonade? Enter S, L or 0 for no drinks:\t'))
        if drink == '0': exit()
        elif drink =='s' or drink =='l':           
            size_input = str.lower(input('What size would you like?  Enter S, M or L\t'))
            size = get_size(size_input)
            price = get_price(size)
            sugar = get_sugar(drink)
            drink_name = get_drink_name(drink)
            drinks=write_drink_list(drink,size,price,sugar)            
            if again == 'y':
                total += 1 
            again = input('Would you like to create another drink? y/n?\t')
        else:
            print("You made an error.")
            again = input('Would you like to create another pie? y or n?\t')

    show_products(total,drinks)
    closing(total)
    
if __name__ == "__main__":
        main()            

In the above code, I tried to use append to add the object to the list but it ends up only having one element in the list. I am not sure if there is something that I miss in my code so the drinks list doesn't add up. If you can point it out, that'll be greatly appreciated.

Below is the result that I got:

Soda or a lemonade? Enter S, L or 0 for no drinks: s

What size would you like? Enter S, M or L s

Would you like to create another drink? y/n? y

Soda or a lemonade? Enter S, L or 0 for no drinks: l

What size would you like? Enter S, M or L l

Sweetened or Unsweetened? Enter S or U: s

Would you like to create another drink? y/n? y

Soda or a lemonade? Enter S, L or 0 for no drinks: l

What size would you like? Enter S, M or L m

Sweetened or Unsweetened? Enter S or U: s

Would you like to create another drink? y/n? n

Drink 3: Soda

Size: Medium Price: $ 1.5 Sugar: Sweetened

You created 3 drink orders. Thanks for using this program!

Aucun commentaire:

Enregistrer un commentaire