Unit 6
Source Code
def main():
print("Main Function")
print("Menu")
print("E1 - Example 1")
print("Q - Quit")
choice = input("Choice: ")
match choice:
# case for Example 1
case "E1":
example1()
# case for Example 2
case "E2":
example2()
# case for Example 3
case "E3":
example3()
case "E3a":
example3a()
# case for Example 4
case "E4":
example4()
# case for Example 5
case "E5":
example5()
# case for Quit
case "Q":
print("Quitting!")
# default case
case _:
print("Invalid, try again!")
# define Example 1 Function
def example1():
print("Example 1")
cs_terms = {
'Algorithm': 'A step-by-step process to solve a problem.',
'Data Structure': 'A way to organize and store data efficiently.',
'Big O Notation': 'Notation expressing algorithm complexity.',
'Recursion': 'A function calling itself to solve a problem.',
'Encapsulation': 'Bundling data and methods in a single unit.',
}
# use a loop to print the keys and values
print("Key\tValue")
for key in cs_terms:
print(key + "\t" + cs_terms[key])
# number of elements
print("Number of Elements: " + str(len(cs_terms)))
# convert keys to a list
cs_keys = list(cs_terms.keys())
print("Keys: " + str(cs_keys))
# convert values to a list
cs_values = list(cs_terms.values())
print("Values: " + str(cs_values))
# define Example 2 Function
def example2():
print("Example 2")
# create an empty dictionary
states = {}
# add a key/value pair
states["NJ"] = "New Jersey"
# print the value of states
print(states)
# add another key/value pair
states["NY"] = "New York"
print(states)
# delete a key/value pair
del states["NY"]
print(states)
# search for a key
search = input("Enter a state abbreviation: ")
if search in states:
print(search + " is an abbreviation for " + states[search])
else:
print(search + " is not in the dictionary")
def example3():
print("Example 3")
# declare gradebook
gradebook = {
'Alice' : [92, 85, 100],
'Bob' : [83, 95, 79],
'Charlie' : [97, 91, 92],
}
# initialize accumulator variables
grades_total = 0
grades_count = 0
# for loop to traverse the grades for each student
for name, grades in gradebook.items():
total = sum(grades)
print("Average for " + name + " " + str(total/len(grades)))
grades_total += total
grades_count += len(grades)
# print the class average
print("Class average is: " + str(grades_total/grades_count))
# define Example 4 Function
def example4():
print("Example 4")
towns = {
'Elizabeth': {'Population': 128885, 'County': 'Union'},
'Hoboken': {'Population': 50000, 'County': 'Hudson'},
'Asbury Park': {'Population': 15860, 'County': 'Monmouth'}
}
# print Hoboken's Population
print("Hoboken's Population: " + str(towns["Hoboken"]["Population"]))
# update Hoboken's Population
towns["Hoboken"]["Population"] = 50001
# add an element
towns["Newark"] = {'Population': 305344, 'County': 'Essex'}
# Delete an element
del towns["Asbury Park"]
# print the dictionary used nested loops
for town, details in towns.items():
print("Town: " + town)
for info, value in details.items():
print(info + ": " + str(value))
print()
# define Example 5 Function
def example5():
print("Example 5")
# create a set
set1 = {'a', 'b', 'c'}
set2 = set(['c', 'd', 'e'])
# traverse a set
for s in set1:
print(s)
# set operations
print("Union: " + str(set1 | set2))
print("Intersection: " + str(set1 & set2))
print("Difference: " + str(set1 - set2))
# symmetric difference
print(set1.symmetric_difference(set2))
# define Example 6 Function
def example6():
print("Example 6")
set1 = {1, 2, 3, 4}
set2 = {3, 4}
# issubset()
print(set1.issubset(set2))
print(set2.issubset(set1))
# issuperset()
print(set1.issuperset(set2))
print(set2.issuperset(set1))
main()
project1.py
import math
def main():
print("Welcome to My Restaurant") # replace with the name of your restgaurant
firstName = "" # add your first name
lastName = "" # add your last name
print("Name: " + firstName + " " + lastName)
print("CPS 3320 - Project 1")
# dictionary to store the user's order.
myOrder = {}
done = False
while not done:
print("Menu")
print("R - Restaurant Information")
print("A - Appetizers")
print("E - Entrees")
print("D - Desserts")
print("B - Beverages")
print("V - View Order")
print("M - Modify Order")
print("P - Place Order")
print("V - View Order from CSV file")
print("Q - Quit")
choice = input("Choice: ").upper()
if choice == "Q":
print("Quit!")
done = True
elif choice == "R":
information()
elif choice == "A":
appetizers(myOrder)
elif choice == "E":
entrees(myOrder)
elif choice == "D":
desserts(myOrder)
elif choice == "B":
beverages(myOrder)
elif choice == "V":
viewOrder(myOrder)
elif choice == "M":
modifyOrder(myOrder)
elif choice == "P":
placeOrder(myOrder)
else:
print("Invalid Choice")
# define restaurant information function
def information():
print("Restaurant Information")
# define appetizers function
def appetizers(myOrder):
print("Appetizers Menu")
# define entrees function
def entrees(myOrder):
print("Entrees Menu")
# define desserts function
def desserts(myOrder):
print("Desserts Menu")
# define beverages function
def beverages(myOrder):
print("Beverages Menu")
# define viewOrder function
def viewOrder(myOrder):
print("View Order")
# define modifyOrder function
def modifyOrder(myOrder):
print("Modify Order")
# define placeOrder function
def placeOrder(myOrder):
print("Place Order")
# define viewOrder function
def viewOrder():
print("View Order")
# call to main function, do not delete!
main()
Last updated