Sitemap

Employee set Project Task 2: Fixing the broken code Python Programming Worksheet

Objective: You will work step by step to create and fix a booking system for Recoats Adventure Park. This system will collect user information, calculate ticket costs, apply discounts, and display a booking summary. You will test the program using the provided structure and ensure it works correctly.

Part 1: Concept Lessons

Before we dive into the booking system, we will learn some fundamental Python concepts that you need to understand to solve the task. Each concept will be followed by an exercise to help you grasp it.

Functions

Functions are reusable blocks of code that perform a task. They can take parameters and return values. If you need to do more examples on this look at this https://medium.com/@icodewithben/python-lesson-understanding-functions-eb43c89fc5e7

Example:

def calculate_total(price, quantity): 
return price * quantity

Exercise: Create a function that calculates the price of items given the unit price and quantity.

While Loops

While loops are useful when you need to keep asking the user for information until they provide valid input.

Example:

while True: 
answer = input("Do you want to quit? (yes/no): ").lower()
if answer == "yes":
break

Exercise: Create a loop that keeps asking the user for a password until it matches a predefined one.

Input Validation

Input validation ensures that the user provides valid data. If the user enters incorrect data, the program should ask them to try again.

Example:

def get_valid_number(): 
while True:
num = input("Enter a number: ")
if num.isdigit():
return int(num)
else:
print("Invalid input. Please enter a number.")

Exercise: Write a function that asks the user for their age and ensures it is a valid number.

Exception Handling

Exception handling is necessary to manage potential errors during program execution, like when converting user input to an integer.

Get Icodewithben’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

try: 
num = int(input("Enter a number: "))
except ValueError:
print("That's not a valid number.")

Exercise: Write a program that asks the user for a number and catches the error if they enter text.

Dictionaries:

Concept: Storing key-value pairs for data management.

Example:

student = {"name": "John", "age": 18} print(student["name"])

Exercise: Create a dictionary storing a product’s name, price, and quantity.

Part 2: The Booking System

Now that you’ve reviewed the basic concepts, you will apply them in a booking system program. You will fix some issues in the code and create a test plan to ensure the system works correctly.

Booking System Code:

You have been given the following incomplete and buggy code. Your task is to correct the errors and ensure the program meets the task requirements. Here is what the code (below) should do:

from datetime import datetime



def get_email ():
flag = True

while flag:
email = input ('Please enter your email address: ')
if "@" not in email or not "." in email:
print("The email entered did not match expected format. Please try again")
flag = True
else:
flag = False

return email

def get_booking_date():
now = datetime.now()
booking_date = now.date()

return booking_date

def get_entry_date():

flag = True

while flag:
user_date = input('Please enter date you wish to visit Recoats Adventure Park (DD/MM/YYYY): ')

try:
datetime.strptime(user_dat, "%d/%m/%Y").date()
except:
print("Sorry, you did not enter a valid date")
flag = True
else:
return datetime.strptime(user_date, "%d/%m/%Y").date()


def get_ticket_numbers (ticket_type):

flag = True

while True:
num_tickets = input('Please enter the number of {} tickets required: '.format(ticket_type))

try:
int(num_tickets)
except:
print("Sorry, you did not enter number")
flag = True
else:
return num_tickets

#checks that the age of junior ticket holders is 16 or under
def check_age (num_junior):

not_junior = 0

for i in range(num_junior):
flag = True

while flag:
age = input('Please enter the age for junior ticket {} : '.format(i))

try:
int(age)
except:
print("Sorry, you did not enter number")
flag = True
else:
if int(age) < 16:
# not_junior = not_junior + 1
flag = False
else:
print("Sorry you have entered an age over 16. This ticket has been chnaged to an adult ticket")
not_junior = not_junior + 1
flag = False

return not_junior

def calculate_subtotal (adults,juniors):
adult_subtotal = adults * 17.50
junior_subtotal = juniors + 11.00

subtotal = adult_subtotal + junior_subtotal

summary = dict(num_adults = adults, cost_adults = adult_subtotal,
num_junior = juniors, cost_juniors = junior_subtotal,
before_discount = subtotal)

return summary

def calculate_total (subtotals):
total_tickets = subtotals.get("num_adults") + subtotals.get("num_junior")
subtotals.update({"num_tickets": total_tickets})

if total_tickets >= 8:
discount = 15
else:
discount = 0

temp_subtotal = subtotals.get("before_discount")
total_with_discount = temp_subtotal - (temp_subtotal * discount)

subtotals.update({"total" : total_with_discount})

return subtotals

def output_summary(final_total, name, email, order_date, entry_date):
print("###################################")
print("## Order Summary ##")
print("===================================")
print("Name: {}".format(name))
print("Email: {}".format(email))
print("Date booked: {}".format(order_date))
print("Date of entry: {}".format(entry_date))
print("Number of Adult Tickets: {}".format(final_total.get("num_adults")))
print("Number of Junior Tickets: {}".format(final_total.get("num_adults")))
print("Total tickets ordered: {}".format(final_total.get("num_tickets")))
print("Toatal before discount: £{}".format(final_total.get("before_discount")))
print("Total after discount: £{}".format(final_total.get("total")))

name = input ("Please enter a name for the booking: ")
email = get_email()
order_date = get_booking_date()
entry_date = get_entry_date()

adult_tickets = get_ticket_numbers ("adult")
junior_tickets = get_ticket_numbers("junior")

not_junior = check_age(junior_tickets)

final_adult_tickets = adult_tickets + not_junior
final_junior_tickets = junior_tickets - not_junior

subtotals = calculate_subtotal(final_adult_tickets, final_junior_tickets)

final_total = calculate_total (subtotals)

print(output_summary(final_total, name, email, order_date, entry_date))

Part 3: Testing the Program

You will now create a test plan to ensure your program works as expected.

Test Table Example:

Here is an example of one test case. You will be responsible for creating similar tests for the rest of the program’s functionality.

Press enter or click to view image in full size

Task:

  • Fill out the rest of the test cases for different parts of the program such as email validation, ticket calculation, and summary output.