By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

A practice script I wrote a while back.


Gold Coder
A little practice script I wrote a couple years ago. I could probably do a little better job of it now. Anyway here is drinks.py for anyone that may want a practice script.

#! /usr/bin/env python3

# Do the imports
from subprocess import call
import os

# Clears the screen of cluttered text
call('clear' if os.name == 'posix' else 'cls')

# Define Menu class and function/method
class Menu:
    def __init__(self):
    def menu(self):
        self.menuitems = {'coffee':1.25, 'tea': 1, 'coke':1.75, 'water':.5}
        return self.menuitems

# Define tax class and function/method
class AddTax:
    def add_tax(self, total, tax):
        return total*tax

# Define discount class and function/method
class Discount:
    def discount(self, total, discount_amount=0):
        return total*discount_amount

# class and function/method for displaying text
class Display:
    def output(self, text):

# Controller text for various operations
class Controller:
    def __init__(self, menu=None, addtax=None, discount=None, display=None):
        self.menu = menu or Menu()
        self.addtax = addtax or AddTax()
        self.discount = discount or Discount()
        self.display = display or Display()

    # Function/method for displaying menu items
    def show_menu(self):
        stuff = []
        for key, val in self.menu.menu().items():
            stuff.append(f'{key}: ${format(val, "0.2f")}')
        self.display.output(f'Menu: {", ".join(stuff)}')

# Define main
def main():
    # Initiate the controller class
    controller = Controller()

    # Start the loop
    while True:
            # Print out message
            print('What would you like to drink?')
            print('Format is item amount seperated by comma or space.')
            print('Example: coke 3, tea 1')
            # Add a blank line

            # Show the menu

            # Ask for order. Can be comma seperated or space
            # Examples coke 2, tea 2 or coke 2 tea 2
            order = input('>> ').lower().replace(',',' ').split()

            # Start a loop for asking if a member. Returns either True or False
            while True:
                print('Are you a member? (y/n)')
                member = input('>>> ')
                if member.strip() == 'y' or member.strip() == 'yes':
                    membership = True
                    membership = False

            # Prints the top part of receipt
            print(f"{''.ljust(25,'-')} Receipt {''.rjust(25,'-')}")

            # This check to make sure that an item and amount is entered
            # If not throws error and ask for correct input
            if order and len(order) >= 2:

                # Places the input into a dict for later use
                iters = iter(order)
                result = dict(zip(iters, iters))

                # Iniate a couple of list for storing
                total = []
                combined_total = []

                # Loop through our input and check if items are in our menu
                # Display accordingly
                for menuitem, amount in result.items():
                    if menuitem in controller.menu.menu().keys():

                        # Stores the cost of items .Gets the cost.
                        # Gets the cost
                        # Formats totalcost for display
                        # Stores combined total for item groups
                        cost = controller.menu.menu()[menuitem]
                        totalcost = f"{format(cost*int(amount.replace(',', ' ')), '0.2f')}"

                        # Display items ordered
                        if len(menuitem) < len(max(result)):
                            npad = (len(max(result))-len(menuitem))+len(min(result))
                            npad = len(max(result))

                        controller.display.output(f'Item: {" ":>4} {menuitem} {"".rjust(npad," ")}Amount: {amount.replace(",","")} {" ":>2} Cost: ${format(cost, "0.2f")} {" ":>2} Total: ${totalcost}')
                        controller.display.output(f'Item: {menuitem} is not on the menu.')
                # Print a blank line

                # Get a total cost of all ordered items
                total = sum(combined_total)

                # If a member add the discount
                if membership == True:
                    discount = controller.discount.discount(total, 0.15)
                    discount = 0

                # Get a new total with the discount
                newtotal = total-discount

                # Get the tax on our total
                tax = controller.addtax.add_tax(newtotal, 0.09)

                # Add the tax to our total
                finaltotal = newtotal+tax

                # Display the content
                controller.display.output(f'Total: {"".rjust(3," ")} ${format(total,"0.2f")}')
                controller.display.output(f'Discount: -${format(discount, "0.2f")}')
                controller.display.output(f'Total: {"".rjust(3," ")} ${format(newtotal,"0.2f")}')
                controller.display.output(f'Tax: {"".rjust(4," ")} +${format(tax,"0.2f")}')
                controller.display.output(f'Total: {"".rjust(3," ")} ${format(finaltotal,"0.2f")}')
                print(f'{"".rjust(59, "-")}')
                print('You will need to enter both item and amout.')
        except ValueError as error:
            print('Please use the correct format when ordering.')

if __name__ == '__main__':

What would you like to drink?
Format is item amount seperated by comma or space.
Example: coke 3, tea 1

Menu: Coffee: $1.25, Tea: $1.00, Coke: $1.75, Water: $0.50
>> Coffee 3, Coke 2, Water 1
Are you a member? (y/n)
>>> y

------------------------- Receipt -------------------------
Item:      Coffee      Amount: 3    Cost: $1.25    Total: $3.75
Item:      Coke        Amount: 2    Cost: $1.75    Total: $3.50
Item:      Water      Amount: 1    Cost: $0.50    Total: $0.50

Total:     $7.75
Discount: -$1.16
Total:     $6.59
Tax:      +$0.59
Total:     $7.18

New Threads

Latest posts

Buy us a coffee!

Top Bottom