Skip to content

Pizza order python script #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions Pizza Order/PizzaTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import subprocess


def runTest (file, inText):
if file =="order":
res = subprocess.run("python " + file + ".py", input=inText, capture_output=True, text=True, shell=True)
return res.stdout

if file == "pizzaReceipt":
res = subprocess.run("python -c \"from pizzaReceipt import *; generateReceipt([" +inText+ "])\"", capture_output=True, text=True, shell= True)
return res.stdout


def equalWithoutSpaces(expected, student):
expected = expected.replace(" ", "")
expected = expected.replace("\t", "")
student = student.replace(" ", "")
student = student.replace("\t", "")
return expected == student



# --------------- Test 1 - No Order---------------

inputString = ""
studentOutput = runTest("pizzaReceipt", inputString)
expectedOutput = "You did not order anything\n"

# Compare studentOutput to expectedOutput
if studentOutput == expectedOutput:
print("Test 1 Passed. (Receipt for empty order)")
else:
print("Test 1 Failed. (Receipt for empty order)")

#print(studentOutput)

# --------------- Test 2 - List of Orders---------------

inputString = "('L', ['HAM', 'BACON', 'ONION', 'TOMATO']), ('S', ['PEPPERONI', 'SAUSAGE', 'CHICKEN', 'HAM']), ('L', ['BROCCOLI', 'CHICKEN', 'ONION'])"
studentOutput = runTest("pizzaReceipt", inputString)
expectedOutput = "Your order: \nPizza 1: L 11.99\n- HAM\n- BACON\n- ONION\n- TOMATO\nExtra Topping (L) 1.00\n"
expectedOutput += "Pizza 2: S 7.99\n- PEPPERONI\n- SAUSAGE\n- CHICKEN\n- HAM\nExtra Topping (S) 0.50\n"
expectedOutput += "Pizza 3: L 11.99\n- BROCCOLI\n- CHICKEN\n- ONION\nTax: 4.35\nTotal: 37.82\n"

# Compare studentOutput to expectedOutput
#if studentOutput == expectedOutput:
if equalWithoutSpaces(expectedOutput, studentOutput):
print("Test 2 Passed. (Receipt for empty order of 3 pizzas)")
else:
print("Test 2 Failed. (Receipt for empty order of 3 pizzas)")


# --------------- Test 3 - List of Orders---------------

inputString = "('XL', ['GREEN PEPPER', 'HOT PEPPER', 'MUSHROOM', 'ONION', 'SPINACH']), ('L', ['PEPPERONI', 'ONION', 'OLIVE', 'MUSHROOM']), ('L', ['PINEAPPLE', 'HAM']), ('M', ['GROUND BEEF', 'TOMATO', 'ONION', 'SPINACH'])"
studentOutput = runTest("pizzaReceipt", inputString)
expectedOutput = "Your order: \nPizza 1: XL 13.99\n- GREEN PEPPER\n- HOT PEPPER\n- MUSHROOM\n- ONION\n- SPINACH\nExtra Topping (XL) 1.25\n"
expectedOutput += "Extra Topping (XL) 1.25\nPizza 2: L 11.99\n- PEPPERONI\n- ONION\n- OLIVE\n- MUSHROOM\n"
expectedOutput += "Extra Topping (L) 1.00\nPizza 3: L 11.99\n- PINEAPPLE\n- HAM\nPizza 4: M 9.99\n"
expectedOutput += "- GROUND BEEF\n- TOMATO\n- ONION\n- SPINACH\nExtra Topping (M) 0.75\nTax: 6.79\nTotal: 59.00\n"

# Compare studentOutput to expectedOutput
if equalWithoutSpaces(expectedOutput, studentOutput):
print("Test 3 Passed. (Receipt for empty order of 4 pizzas)")
else:
print("Test 3 Failed. (Receipt for empty order of 4 pizzas)")

# --------------- Test 4 - Find Specific Values in Output ---------------

studentOutput = runTest("order", "Yes\nL\nHAM\nX\nNo\n")

if studentOutput.find("13.55") != -1:
print("Test 4 Passed. (Ordering system for order of 1 pizza)")
else:
print("Test 4 Failed. (Ordering system for order of 1 pizza)")

#print(studentOutput)

# --------------- Test 5 - Find Specific Values in Output---------------

studentOutput = runTest("order", "Yes\nmedium\nM\nLIST\npepperoni\nonion\nmushroom\nhot pepper\ntomato\nX\nq\n")

if studentOutput.find("\"X\"\n('") != -1 and studentOutput.count("Choose a size:") == 2 and studentOutput.count("Type in one of our toppings") == 7 and studentOutput.find("1.49") != -1 and studentOutput.find("12.98") != -1:
print("Test 5 Passed. (Ordering system with typo and use of LIST)")
else:

print("Test 5 Failed. (Ordering system with typo and use of LIST)")

print()
print(studentOutput.find("\"X\"\n('") != -1)
print(studentOutput.count("Choose a size:") == 2)
print(studentOutput.count("Type in one of our toppings") == 7)
print(studentOutput.find("1.49") != -1)
print(studentOutput.find("12.98") != -1)
print()

# --------------- Find Specific Values in Output ---------------

studentOutput = runTest("order", "y\nm\nsausage\nbacon\nonion\nX\ny\nXl\nchicken\ntomato\nspinach\nmushroom\nx\ny\nm\nolive\nbroccoli\nhot pepper\ngreen pepper\nx\nno\n")

if studentOutput.count("Type in one of our toppings") == 14 and studentOutput.find("4.68") != -1 and studentOutput.find("40.65") != -1:
print("Test 6 Passed. (Ordering system for order of 3 pizzas)")
else:
print("Test 6 Failed. (Ordering system for order of 3 pizzas)")

# print(studentOutput)
# print(expectedOutput)
# --------------------------------




6 changes: 6 additions & 0 deletions Pizza Order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# OrderPizza
A program that I created in one of my computer science courses containing a frontend and backend. The frontend deals with asking the user for their order, while the backend focuses on storing the information in a receipt format along with its calculations by using functions, lists, and tuples.

In the frontend, the user first asks if the customer would like to order. If they say no, the code exits completely and is terminated. If the user says yes, it will then ask for the size of pizza as each have a different price range. After, the user will ask for toppings. The maximum that they can enter is three. Afterwards, every topping will cost an additional price to its total. After they have completed asking toppings, the program creates the order in a tuple and is stored in a variable. If the user would like to continue to ordering, the program would loop once more using a while loop and repeat the same questions that were asked in the last instance Afterwards, the second order is incremented to the variable containing the tuple. Otherwise, the code exits and the pizza receipt file is called using the variable of the tuple containing the pizza order and will print a receipt detailing the order and the total.

In the backend, once the customer has finished ordering, the function is called using the variable as a paramter containing the customers order and creates a receipt by first showing the first pizza, its size, toppings and the amount is placed on the right hand side. If there are more than three toppings, it will show its additional cost as well. If there are more pizzas included, it will repeat the process once more. Afterwards, it shows the total before tax, the additional 13% tax in dollar amount and the total with tax.
80 changes: 80 additions & 0 deletions Pizza Order/order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# purpose of order.py is the front end for users to submit their order
from pizzaReceipt import * # asks to import all functions found in the pizzaReceipt.py file

# set all initial variables before beginning
size = ""
pizza_lst = []
pizza_lst_current = []
toppings_lst = [] # list to be a parameter for generateReceipt function
list_order_yes = ["Yes", "yes", "Y", "y", "YES"]
list_order_no = ["No", "no", "Q", "NO", "N", "n"]
TOPPINGS = ("ONION", "TOMATO", "GREEN PEPPER", "MUSHROOM", "OLIVE", "SPINACH", "BROCCOLI", "PINEAPPLE",
"HOT PEPPER", "PEPPERONI", "HAM", "BACON", "GROUND BEEF", "CHICKEN", "SAUSAGE")

# ask user whether they want to order.
order = input("Do you want to order a pizza? ")

# case for when an invalid input is submitted
while (order not in list_order_yes) and (order not in list_order_no):
order = input("Do you want to order a pizza? ")

# ask for size
if order in list_order_yes:
size = input("Choose a size: ")
size.upper()

# case for when a user inputs invalid size
while size.upper() not in ["S", "M", "L", "XL"]:
size = input("Choose a size: ")

# entire loop to repeat if user wants to order more than one pizza
while order in list_order_yes:
# set empty toppings list as it will show empty each time loop is made
toppings_lst = []
# ask user for topping, whether they want to see a list of the toppings, or to finish ordering toppings.
topping = input('Type in one of our toppings to add it to your pizza. To see the list of toppings, enter "LIST". '
'When you are done adding toppings, enter "X" ')

# cae for when a user places an invalid input for this question
while (topping.upper() != "X") and (topping.upper() != "LIST") and (topping.upper() not in TOPPINGS):
topping = input('Type in one of our toppings to add it to your pizza. To see the list of toppings, enter "LIST". '
'When you are done adding toppings, enter "X" ')

print()
# toppings while loop which ask for toppings selection or list view, multiple times until user enters X
while topping.upper() != "X":
TOPPINGS = ("ONION", "TOMATO", "GREEN PEPPER", "MUSHROOM", "OLIVE", "SPINACH", "BROCCOLI", "PINEAPPLE",
"HOT PEPPER", "PEPPERONI", "HAM", "BACON", "GROUND BEEF", "CHICKEN", "SAUSAGE")
if topping.upper() == "LIST":
print(topping.upper())
print(TOPPINGS)
topping = input('Type in one of our toppings to add it to your pizza. To see the list of toppings, enter "LIST". '
'When you are done adding toppings, enter "X" \n \n')
elif topping.upper() in TOPPINGS:
print(topping.upper())
print("Added", topping.upper(), "to your pizza")
toppings_lst.append(topping)
topping = input('Type in one of our toppings to add it to your pizza. To see the list of toppings, enter "LIST". '
'When you are done adding toppings, enter "X" \n \n')

# add the size and toppings list as a tuple to pizza_lst
pizza_lst.append((size.upper(), toppings_lst))
print(pizza_lst)
# ask whether they want to continue ordering
order = input("Do you want to continue ordering? ")

# case for when user types invalid input
while (order not in list_order_yes) and (order not in list_order_no):
order = input("Do you want to order a pizza? ")

# if they say no, break the loop and go through the last line of the code
if order in list_order_no:
break
elif order in list_order_yes:
size = input("Choose a size: ") # if they want to order again start by asking size

# case for when user types invalid input
while size.upper() not in ["S", "M", "L", "XL"]:
size = input("Choose a size: ")

generateReceipt(pizza_lst) # at the end of program, call function using pizza_lst as a parameter
105 changes: 105 additions & 0 deletions Pizza Order/pizzaReceipt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# purpose of pizzaReceipt.py is the back end where the developers create methods and functions to properly format the receipt and its total
def generateReceipt(pizzaOrder):
# parameter will be placed with pizza_lst = ("M", ["PEPPERONI", "OLIVE"], -----)
# set initial variables
size = ""
additional_price = 0
price_before_tax = 0
tax = 1.13
counter = 1 # pizza number
size_price = 0
additional_price_tag = float(0)
additional_price_tag_format = ""

# if its an empty list, display this statement
if len(pizzaOrder) == 0:
print("You did not order anything")
exit()

# beginning of the format of receipt
print("Your order: ")

# a for loop which goes through all tuples in the list based on its indices
for pizza in range(len(pizzaOrder)):
# cases to determine the sizes selected and its price
if pizzaOrder[pizza][0] == "S":
size_price = 7.99
size = "S"
elif pizzaOrder[pizza][0] == "M":
size_price = 9.99
size = "M"
elif pizzaOrder[pizza][0] == "L":
size_price = 11.99
size = "L"
elif pizzaOrder[pizza][0] == "XL":
size_price = 13.99
size = "XL"

# add the price of the size to the final price before tax
price_before_tax += size_price

# formatting the pizza number and its size beside it and the price on the other side
if size == "XL":
print("Pizza", str(counter) + ":", str(size) + " \t\t\t " + str(size_price))
elif size == "L":
print("Pizza", str(counter) + ":", str(size) + " \t\t\t " + str(size_price))
elif size == "M":
print("Pizza", str(counter) + ":", str(size) + " \t\t\t " + str(size_price))
elif size == "S":
print("Pizza", str(counter) + ":", str(size) + " \t\t\t " + str(size_price))

# increment counter variable by one for the pizza number
counter += 1

# format the toppings with a dash in front
for j in range(len(pizzaOrder[pizza][1])):
print("- " + str(pizzaOrder[pizza][1][j]))

# if theres more than three toppings, calculate the total additional price and added to the total price before tax
if len(pizzaOrder[pizza][1]) > 3:
n = len(pizzaOrder[pizza][1]) - 3
if size == "S":
additional_price_tag = 0.50
additional_price_tag_format = "{:.2f}".format(additional_price_tag)
additional_price = 0.50 * n
price_before_tax += additional_price
elif size == "M":
additional_price_tag = 0.75
additional_price = 0.75 * n
price_before_tax += additional_price
elif size == "L":
additional_price_tag = 1.00
additional_price_tag_format = "{:.2f}".format(additional_price_tag)
additional_price = 1.00 * n
price_before_tax += additional_price
elif size == "XL":
additional_price_tag = 1.25
additional_price = 1.25 * n
price_before_tax += additional_price

# format the extra topping portion of the receipt with its size and price on the other side
float_additional_price = float(additional_price)
format_additional_price = "{:.2f}" .format(float_additional_price)

for extra in range(len(pizzaOrder[pizza][1])):
if extra > 2:
if size == "XL":
print("Extra Topping", "(" + size + ")" + "\t\t " + str(additional_price_tag))
elif size == "L":
print("Extra Topping", "(" + size + ")" + "\t\t " + str(additional_price_tag_format))
elif size == "M":
print("Extra Topping", "(" + size + ")" + "\t\t " + str(additional_price_tag))
elif size == "S":
print("Extra Topping", "(" + size + ")" + "\t\t " + str(additional_price_tag_format))
# outside of loop begins, calculates the price before tax with the tax value set earlier
price_final = price_before_tax * tax

# add the tax price to be added and set it to a float, as well as the final price with tax
float1 = float(price_before_tax * (13/100))
float2 = float(price_final)
formatFloat1 = "{:.2f}" .format(float1)
formatFloat2 = "{:.2f}" .format(float2)

# format the price of the tax, and the total with both values on the other side
print("Tax:" + "\t\t\t\t\t " + formatFloat1)
print("Total:" + "\t\t\t\t\t " + formatFloat2)
8 changes: 8 additions & 0 deletions Pizza Order/pyvenv.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
home = /Library/Frameworks/Python.framework/Versions/3.10
implementation = CPython
version_info = 3.10.0.final.0
virtualenv = 20.13.0
include-system-site-packages = false
base-prefix = /Library/Frameworks/Python.framework/Versions/3.10
base-exec-prefix = /Library/Frameworks/Python.framework/Versions/3.10
base-executable = /usr/local/bin/python3.10
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ More information on contributing and the general code of conduct for discussion
| PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. |
| PDF to Text | [PDF to text](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20text) | Converts PDF to text. |
| PDF merger and splitter | [PDF Merger and Splitter](https://github.com/AbhijitMotekar99/Python-Scripts/blob/master/PDF%20Merger%20and%20Splitter/PDF%20Merger%20and%20Splitter.py) | Create a tool that can merge multiple PDF files into one or split a single PDF into separate pages. |
| Pizza Order | [Pizza Order](https://github.com/DhanushNehru/Python-Scripts/tree/master/Pizza%20Order) | An algorithm designed to handle pizza orders from customers with accurate receipts and calculations. |
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun. |
| Playlist Exchange | [Playlist Exchange](https://github.com/DhanushNehru/Python-Scripts/tree/master/Playlist%20Exchange) | A Python script to exchange songs and playlists between Spotify and Python. |
| Pigeonhole Sort | [Algorithm](https://github.com/DhanushNehru/Python-Scripts/tree/master/PigeonHole) | The pigeonhole sort algorithm to sort your arrays efficiently! |
Expand Down