5
\$\begingroup\$

I am new to coding/programming. About four months ago I decided to develop my skills using Python. After several books and a lot of assistance from this awesome community I developed the first part of my "Pizza Order System" program.

Explanation of my program:

When the program in ran, the user is prompted to pick a pizza size. The user can choose more until they press t which will return the total of all the pizzas chosen. The program then continues to the topping menu where the user can choose toppings ($1 each). When they are finished choosing their toppings the user will press f and the program will return the final order total.

size_mappings = {
    1: "Small",
    2: "Large",
    3: "Extra Large",
    4: "Party Size"
}


cost_mappings = {
    "Small": 6,
    "Large": 10,
    "Extra Large": 12,
    "Party Size": 24
}



class Pizza():
    def __init__(self, size):
        self.size = size

    def set_size(self, size):
        self.size = size

    def get_size(self):
        return self.size()

    def get_cost(self):
        return cost_mappings[self.size]



class Order():
    def __init__(self):
        self.pizzas = []

    def addPizza(self, pizza):
        self.pizzas.append(pizza)

    def getTotal(self):
        total = 0
        for pizza in self.pizzas:
            total += pizza.get_cost()
        return total

# start processing the order
order = Order()


def run():
    print("\nWhat size pizza would you like?\n\n\
    _____________________________________________________________\n\
    | 1: Small  |  2: Large  |  3: Extra Large  |  4: Party Size |\n\
    |    $6     |     $10    |        $12       |       $24      |\n\
    |___________|____________|__________________|________________|\n\
    \n- Press 't' to choose your toppings\n")

    while True:
        try:
            
            response = input('-')

            if response == 't':
                break

            size_wanted = int(response)

            size_wanted = size_mappings[size_wanted]
            

            print(f"Pizza: {size_wanted}")
            order.addPizza(Pizza(size_wanted))
        except:
            print("An error occurred, please try again")
run()

print("your current order total: ", "$" + str(order.getTotal()))


topping_mappings = {
                    1: 'Anchovies', 
                    2: 'Bacon', 
                    3: 'Bell Peppers', 
                    4: 'Black Olives', 
                    5: 'Chicken', 
                    6: 'Ground Beef', 
                    7: 'Jalapenos', 
                    8: 'Mushrooms', 
                    9: 'Pepperoni',
                    10: 'Pineapple', 
                    11: 'Spinach',
                    12: 'Onion'
                    }

topping_cost_mappings = {
                        'Anchovies': 1, 
                        'Bacon': 1, 
                        'Bell Peppers': 1, 
                        'Black Olives': 1, 
                        'Chicken': 1, 
                        'Ground Beef': 1, 
                        'Jalapenos': 1, 
                        'Mushrooms': 1, 
                        'Pepperoni': 1,
                        'Pineapple': 1, 
                        'Spinach': 1, 
                        'Onion': 1
                        }


class CustomerToppings():
    """ Have customer pick toppings for pizza"""
    
    def __init__(self, numToppings):
        self.numToppings = numToppings

    def set_toppings(self, numToppings):
        self.numToppings = numToppings

    def get_toppings(self):
        return topping_cost_mappings[self.numToppings]


class ToppingOrder():

    def __init__(self):
        self.topping = []

    def addTopping(self, toppings): 
        self.topping.append(toppings)

    def toppingTotal(self):
        get_topping_total = 0
        for toppings in self.topping:
            get_topping_total += toppings.get_toppings()
        return get_topping_total

# Strat processing the order
topping_order = ToppingOrder()

def runTopping():
    print("\nWhat toppings would you like on your pizza?\n\n\
    ______________________________________________________________________\n\
    | 1: Anchovies |    2: Bacon    | 3: Bell Peppers |  4: Black Olives |\n\
    |  5: Chicken  | 6: Ground Beef |   7: Jalapenos  |   8: Mushrooms   |\n\
    | 9: Pepperoni |  10: Pineapple |   11: Spinach   |    12: Onions    |\n\
    |______________|________________|_________________|__________________|\n\
    Press 'f' for your final total: \n")

    while True:
        try: 
            response = input('-')
            if response == 'f':
                break
            toppings_wanted = int(response)

            toppings_wanted = topping_mappings[toppings_wanted]

            print(f"Topping: {toppings_wanted}")
            topping_order.addTopping(CustomerToppings(toppings_wanted))
        except:
            print("An error occurred, please try again")

runTopping()

sub_size = int(order.getTotal())
sub_toppings =  int(topping_order.toppingTotal())
final_total = sub_size + sub_toppings

print(f" \nYour final total will be ${final_total}\n")
\$\endgroup\$
6
\$\begingroup\$

Welcome to CR community.

  1. Keep constant declarations at the top. Although you follow the PEP8 naming conventions throughout (almost) the whole code base, I'll point out a few key things:

    • variables, functions and methods should be named using snake_case. So, the addTopping would be renamed to add_topping.
    • constant (or globals) are named as UPPER_SNAKE_CASE. So, the size_mappings would become SIZE_MAPPINGS.
    • classes are named as CamelCase. You're already following this convention.
  2. Use triple-quoted strings in python for multiline content. Your print statements would look a lot cleaner (no need for \n\n\n... chains). The following prints the same list/table:

     print("""
     What size pizza would you like?
    
         _____________________________________________________________
         | 1: Small  |  2: Large  |  3: Extra Large  |  4: Party Size |
         |    $6     |     $10    |        $12       |       $24      |
         |___________|____________|__________________|________________|
    
     - Press 't' to choose your toppings
     """)
    
  3. Put the execution flow of your code inside the if __name__ == "__main__" block.

  4. When ordering a pizza, usually I am asked toppings for each pizza separately, instead of me listing down the different sizes of pizzas, and then choosing toppings for those in bulk. This would also change how your program execution goes.

  5. If following the above, Toppings elements would be a list of elements inside the Pizza class.

  6. You do not need a separate ToppingsOrder class.

  7. Instead of maintaining 2 different variables for mapping pizza choice <-> size <-> price (and similarly for toppings), you can use a namedtuple (or dataclass, as per your needs):

     from collections import namedtuple
    
     Pizza = namedtuple("Pizza", ["name", "price"])
     SIZE_MAPPINGS = {
         1: Pizza("Small", 6),
         .
         .
     }
    

    Now, you can add_pizza to an order as simply as:

     order.add_pizza(SIZE_MAPPINGS[size_wanted])
    

    and when fetching price (or name) of pizza, it would be pizza.price (or pizza.name).

\$\endgroup\$
  • \$\begingroup\$ Thank you so much taking the time to review my code, this was very helpful. \$\endgroup\$ – Chris Jaurigue Jul 22 '20 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.