From 97a727c2842d6a09bdde18bfbd4f6bdc3bd35774 Mon Sep 17 00:00:00 2001 From: Arjayn Piratheeparatnam <122946744+ArjaynP@users.noreply.github.com> Date: Sun, 19 Jan 2025 22:59:06 -0500 Subject: [PATCH 1/2] ordering pizza python project This program incorporates fundamental Python programming knowledge. It is designed to take orders made by customers and handle all scenarios. --- pizzaOrder.py | 80 ++++++++++++++++++++++++++++++++++++ pizzaReceipt.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 pizzaOrder.py create mode 100644 pizzaReceipt.py diff --git a/pizzaOrder.py b/pizzaOrder.py new file mode 100644 index 00000000..3ece1594 --- /dev/null +++ b/pizzaOrder.py @@ -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 diff --git a/pizzaReceipt.py b/pizzaReceipt.py new file mode 100644 index 00000000..b7ff1d3d --- /dev/null +++ b/pizzaReceipt.py @@ -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) From ed2d2b14be8218db639822d62899c0f2470c6245 Mon Sep 17 00:00:00 2001 From: Arjayn Piratheeparatnam <122946744+ArjaynP@users.noreply.github.com> Date: Sat, 1 Feb 2025 15:40:02 -0500 Subject: [PATCH 2/2] Create connectfour.py --- connectfour.py | 242 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 connectfour.py diff --git a/connectfour.py b/connectfour.py new file mode 100644 index 00000000..c5fd146e --- /dev/null +++ b/connectfour.py @@ -0,0 +1,242 @@ +import random + +board = [["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6] + + +def gamePlayTwoPlayers(p1, p2): + win = "" + while win == "": + column = int(input("{}'s turn. Enter which column you would like to drop your piece into: ".format(p1))) + connectFourBoard(column, "X") + + if check_vertical_win(p1, p2, column): + print() + print(check_vertical_win(p1, p2, column)) + break + if check_horizontal_win(p1, p2): + print() + print(check_horizontal_win(p1, p2)) + break + if check_positive_diagonal_win(p1, p2): + print() + print(check_positive_diagonal_win(p1, p2)) + break + if check_negative_diagonal_win(p1, p2): + print() + print(check_negative_diagonal_win(p1, p2)) + break + + column = int(input("{}'s turn. Enter which column you would like to drop your piece into: ".format(p2))) + connectFourBoard(column, "O") + + if check_vertical_win(p1, p2, column): + print() + print(check_vertical_win(p1, p2, column)) + break + if check_horizontal_win(p1, p2): + print() + print(check_horizontal_win("x", "O")) + break + if check_positive_diagonal_win(p1, p2): + print() + print(check_positive_diagonal_win(p1, p2)) + break + if check_negative_diagonal_win(p1, p2): + print() + print(check_negative_diagonal_win(p1, p2)) + break + + +def gamePlayOnePlayer(p1, p2): + win = "" + while win == "": + column = int(input("{}'s turn. Enter which column you would like to drop your piece into: ".format(p1))) + connectFourBoard(column, "X") + + if check_vertical_win(p1, p2, column): + print() + print(check_vertical_win(p1, p2, column)) + break + if check_horizontal_win(p1, p2): + print() + print(check_horizontal_win(p1, p2)) + break + if check_positive_diagonal_win(p1, p2): + print() + print(check_positive_diagonal_win(p1, p2)) + break + if check_negative_diagonal_win(p1, p2): + print() + print(check_negative_diagonal_win(p1, p2)) + break + + print() + + column = random.randint(1, 7) + connectFourBoard(column, "O") + + if check_vertical_win(p1, p2, column): + print() + print(check_vertical_win(p1, p2, column)) + break + if check_horizontal_win(p1, p2): + print() + print(check_horizontal_win(p1, p2)) + break + if check_positive_diagonal_win(p1, p2): + print() + print(check_positive_diagonal_win(p1, p2)) + break + if check_negative_diagonal_win(p1, p2): + print() + print(check_negative_diagonal_win(p1, p2)) + break + + +def connectFourBoard(col, playerIcon): + col -= 1 + coordinate = [] + + for row in range(len(board[col])-1, -1, -1): + if board[col][row] == "-": + board[col][row] = playerIcon + coordinate.append([row, col]) + break + for row in range(len(board)): + for col in range(len(board[row])): + print("|", board[row][col], "|", board[row+1][col], "|", board[row+2][col], "|", board[row+3][col], "|", + board[row+4][col], "|", board[row+5][col], "|", board[row+6][col], "|") + break + + +def check_vertical_win(p1, p2, col): + playerCount1 = 0 + playerCount2 = 0 + col -= 1 + + for row in range(len(board[col])-1, -1, -1): + if board[col][row] == "X": + playerCount1 += 1 + playerCount2 = 0 + elif board[col][row] == "O": + playerCount2 += 1 + playerCount1 = 0 + + if playerCount1 == 4: + return "{} Wins".format(p1) + elif playerCount2 == 4: + return "{} Wins".format(p2) + + +def check_horizontal_win(p1, p2): + for row in range(len(board[0])-1, -1, -1): + playerCount1 = 0 + playerCount2 = 0 + for col in range(len(board)): + if board[col][row] == "X": + playerCount1 += 1 + playerCount2 = 0 + elif board[col][row] == "O": + playerCount2 += 1 + playerCount1 = 0 + elif board[col][row] == "-": + playerCount1 = 0 + playerCount2 = 0 + + if playerCount1 == 4: + return "{} Wins".format(p1) + elif playerCount2 == 4: + return "{} Wins".format(p2) + + +def check_positive_diagonal_win(p1, p2): + playerCount1 = 0 + playerCount2 = 0 + + for row in range(len(board[0])-1, -1, -1): + for col in range(len(board)-3): + if board[col][row] == "X": + playerCount1 += 1 + while playerCount1 < 4: + col += 1 + row -= 1 + if board[col][row] == "X": + playerCount1 += 1 + else: + playerCount1 = 0 + break + elif board[col][row] == "O": + playerCount1 += 1 + while playerCount1 < 4: + col += 1 + row -= 1 + if board[col][row] == "O": + playerCount1 += 1 + else: + playerCount1 = 0 + break + + if playerCount1 == 4: + return "{} Wins".format(p1) + elif playerCount2 == 4: + return "{} Wins".format(p2) + else: + playerCount1 = 0 + playerCount2 = 0 + + +def check_negative_diagonal_win(p1, p2): + playerCount1 = 0 + playerCount2 = 0 + + for row in range(len(board[0])-3): + for col in range(len(board)-3): + if board[col][row] == "X": + playerCount1 += 1 + while playerCount1 < 4: + col += 1 + row -= 1 + if board[col][row] == "X": + playerCount1 += 1 + else: + playerCount1 = 0 + break + elif board[col][row] == "O": + playerCount1 += 1 + while playerCount1 < 4: + col += 1 + row += 1 + if board[col][row] == "O": + playerCount1 += 1 + else: + playerCount1 = 0 + break + + if playerCount1 == 4: + return "{} Wins".format(p1) + elif playerCount2 == 4: + return "{} Wins".format(p2) + else: + playerCount1 = 0 + playerCount2 = 0 + + +def main(): + print("Welcome to Connect Four! Connect 4 of your pieces in either horizontal, vertical, or diagonal to win.") + + numPlayers = int(input("Choose how many players: ")) + + while numPlayers not in [1,2]: + numPlayers = int(input("Sorry for the value you entered was invalid. Are you playing with 1 or 2 players: ")) + + if numPlayers == 1: + player1 = input("Player 1, please enter your name: ") + player2 = "CPU" + gamePlayTwoPlayers(player1, player2) + elif numPlayers == 2: + player1 = input("Player 1, please enter your name: ") + player2 = input("Player 2, please enter your name: ") + gamePlayTwoPlayers(player1, player2) + + +main() \ No newline at end of file