Question
Movie ticket discounts revisited
CSW8 Learning Goals
In this lab, you will:
- build on the solution to the 3.15 LAB: Movie ticket discounts
- define and call a function with multiple parameters - you will need to add a new parameter to your previous solution
- use the result of a function as part of the main program - the function will now return a list and a new error code
- practice creating a new list and
append
ing new elements to it - check the type of the variable is a list by using
if type(variableName) == list
Main Idea
This lab is based on the 3.15 LAB: Movie ticket discounts.
Your goal is to write a new function get_ticket_price_discount()
, that builds on the get_ticket_price()
and takes the following parameters:
- a base price (float) - no longer hard-coded in the function
- an age (integer)
- a student status (Boolean)
- a military status (Boolean)
- a first-responder status (Boolean)
The function returns a list that has any discounts that were applied to the resulting price (in the order they were applied) and the computed movie ticket price as the last element of the list.
The function returns error codes if the base price or the age are not valid.
Instructions to write the function
If the base price is 1 or less, the function returns 0
as an integer.
Otherwise, the function sets the ticket base price to be the provided base price parameter.
If the age is not valid, the function returns -1
as an integer.
- If the Age 12 and under, the function subtracts $3 from the base price.
- If the Age 65 and older, the function subtracts $2.
In preparation for applying discounts, the function creates an empty list.
The function then independently checks the provided statuses in the order listed below and applies the following discounts:
- 30% discount to students
- 25% - to military
- 20% - to first-responders
If any of the discounts were applied, the function stores the discount percentage as an integer in a list.
- E.g., if the discount was 30% for a student ticket, the list would store
[30]
. - Make sure you write the the checking of the statuses in the order specified above.
The function returns a list that has all discounts that were applied to the resulting ticket price (in the order that they were applied) and includes the computed movie ticket price (as a float) as the last element of the list.
The function should not be rounding the result.
The main program
Get the price as a float input from the user.
Get the age as an integer input from the user.
Get the necessary statuses as an input string from the user:
- next, collect their response whether they are a student, then if they are military, and then if they are a first-responder.
- If the user input
"yes"
, then convert the variable to the BooleanTrue
; otherwise, set the status toFalse
. - To convert the user input, use the
convert_yes_no()
function that was developed in class.
Before calling the function output a debugging print statement showing the arguments that are going to be passed into the function.
print("Calling the function with", ..., ..., ..., ..., ...)
Call the function get_ticket_price_discount()
with the provided input arguments to get the result.
- Display the value that was returned from the function:
print("The function returned", ...)
After we call the function, we can examine the returned value/object to determine what to output.
- If the function returns a list (check using the
type()
function):- If the list contains a single element:
- Display the output as follows:
print(f'No discounts were applied.\nMovie ticket price: ${...:.2f}')
. - Remember to retrieve/index the first element of the list, otherwise, you will see the
[
square brackets]
in the output.
- Display the output as follows:
- Otherwise, the list has more than one element, which means that the discounts were applied and we can display them.
- Use the
if len(...)
branches to detect how many discounts to output: you will need 3 branches in order to display the result correctly. - Display the output as follows:
print(f'Discount applied: {...}%')
for each discount that is stored in the list. - Finally, display the
print(f'Movie ticket price: ${...:.2f}')
using the last element stored in the list. You can uselist_name[-1]
orlist_name[len(list_name)-1]
substituting your list's name forlist_name
.
- Use the
- If the list contains a single element:
- Otherwise, if the function didn't return a list (i.e., it returned an integer error) display
Invalid input
As before, remember to round the price to 2 decimal places in your main program!
Testing the code
The set up is the same as in the 3.15 LAB: Movie ticket discounts, except that you are providing one more input value - the base ticket price - as the input in the main program, which uses it as the argument into your function.
If we call the new function with the same base price as what we used before, we should see that the returned value should still be the same as what we calculated previously.
A 67-year old veteran (not a student nor a first-responder)
For example, if the input is
14.0
67
no
yes
no
the return value from the function is
[25, 9.0]
and the output from the main program is
Calling the function with 14.0 67 False True False
The function returned [25, 9.0]
Discount applied: 25%
Movie ticket price: $9.00
Troubleshooting / Hints
- check for the validity of the price and then the user age first, before computing any prices.
- Make sure you write the function logic in the order specified by the instructions.
- remember that the function expects Boolean values, not strings "yes" or "no" (as indicated in the documentation)
- To get the last element stored in the list, you can use
list_name[-1]
orlist_name[len(list_name)-1]
substituting your list's name forlist_name
. - The unit test is calling your function with the values that you are outputting as part of your debugging print statements in the main program.
Debugging your code
If you take that output and then give it as the arguments into your function, you can print the result that comes back from the function to see if it is correct.
For example, for the test that is “Calling the function with 14.0 35 True False True
”, you can check what the function returns by hard-coding these values at the end of your main program to see the output:
print("Calling the function with 14.0 35 True False True")
result = get_ticket_price(14.0, 35, True, False, True)
print("The function returned", result)
This is 3.15 code below:
def get_ticket_price(age, is_student, is_military, is_first_responder):
"""
This function takes as an argument
param: age (int) - the age of the customer
param: is_student (bool) - whether the customer is a student
param: is_military (bool) - whether the customer is/was in the military
param: ... (bool) - whether the customer is a first responder
The function returns a floating-point value corresponding to the
computed movie ticket price.
If the age is not a value greater than 0,
the function returns an integer -1.
"""
ticket_price = 14.0
if age <= 0:
return -1
elif age <= 12:
ticket_price -= 3
elif age >= 65:
ticket_price -= 2
if is_student:
ticket_price *= 0.7
if is_military:
ticket_price *= 0.75
if is_first_responder:
ticket_price *= 0.8
return ticket_price
def convert_yes_no(input_string):
if input_string.lower() == "yes":
return True
elif input_string.lower() == "no":
return False
else:
return None
if __name__ == "__main__":
age = int(input())
student_response = input()
is_student = convert_yes_no(student_response)
military_response = input()
is_military = convert_yes_no(military_response)
responder_response = input()
is_responder = convert_yes_no(responder_response)
print("Calling the function with", age, is_student, is_military, is_responder)
result = get_ticket_price(age, is_student, is_military, is_responder)
if result == -1:
print("Invalid age")
else:
print(f"Movie ticket price: ${result:.2f}")
Asked By MidnightDreams92 at
Answered By Expert
Alvin
Expert · 2.1k answers · 2k people helped
Step 1/5
Step 1: Import the necessary libraries and load the dataset
Explanation:
Step 2/5
Step 2: Data preprocessing
Explanation:
Step 3/5
Step 3: Split the dataset into training and testing sets
Explanation:
Step 4/5
Step 4: Train the model
Explanation:
Step 5/5
Step 5: Evaluate the model
Explanation:
Final Answer
Here's a complete solution to the lab:
Here's an explanation of what each part of the code does:
1.) get_ticket_price_discount() function: This function takes in the base ticket price, age, and boolean values for student, military, and first-responder status. It calculates the final ticket price after applying any applicable discounts, and returns a list containing the discount percentages (in the order they were applied) and the final ticket price.
2.) convert_yes_no() function: This function takes in a string value of "yes" or "no" and returns a boolean value of True or False, respectively.
3. ) User input: The program prompts the user to input the base ticket price, age, and boolean values for student, military, and first-responder status. The convert_yes_no() function is used to convert the "yes" or "no" strings to boolean values.
4. ) Debug print statement: Before calling the get_ticket_price_discount() function, the program prints
🧑🏫 More Questions
👉 Interested in exploring further?
Chrome Extension
1. Search answers from our 90+ million questions database.
2. Get instantly AI Solutions powered by most advanced models like GPT-4, Bard, Math GPT, etc.
3. Enjoy one-stop access to millions of textbook solutions.
4. Chat with 50+ AI study mates to get personalized course studies.
5. Ask your questions simply with texts or screenshots everywhere.