python strings methods formatting f-strings 2024 Challenge
Read the problem description and solve the challenge in the workspace.
While there was no "Practice Quiz on Python Strings" included in the uploaded sources I have used the "Tutorial at Python Strings" along with the other provided resources on string formatting to create this practical coding challenge based on a concepts taught.
Coding Challenge: The Event Ticket Generator
Problem Description
You're basically building an automated ticketing system for a local tech conference, while when user purchases tickets, the system needs to generate a custom confirmation receipt.
Using Python's modern f-strings your program must dynamically insert the user's name and event name into a welcome message. Plus, you need to calculate the total cost of their order directly inside string before it prints out!
Difficulty Level
Beginner
Input Specifications
You will be provided with a following predefined variables:
* user_name (string): The name of the customer buying the tickets.
* event_name (string): A name of an event they are basically attending.
* ticket_price (float): The cost about single ticket.
* ticket_quantity (integer): The number of tickets a customer is purchasing.
Output Specifications
Your code must create and print the single confirmation_message string that outputs exactly inside this format:
Hello [user_name], your [ticket_quantity] tickets for [event_name] are confirmed! Total cost: $[calculated total].
Starter Code Boilerplate
# Predefined variables for the ticket order
user_name = "Alice"
event_name = "Python Developer Conference"
ticket_price = 150.00
ticket_quantity = 3
# TODO: Use an f-string to create the confirmation message.
# Make sure to calculate the total cost directly inside the f-string braces!
confirmation_message = ""
# Print the final formatted string
print(confirmation_message)
Hints
- Activating F-Strings: By simply prefixing your string with a lowercase
for the uppercaseF, you activate Python's formatting magic. - Direct Embedding: You can embed your variables directly inside curly braces
{}right where you want them to appear in the text. - Math Inside Strings: Because expressions inside an f-string's curly braces are basically evaluated dynamically at runtime, you aren't limited to just variables; you can put the multiplication equation for the total cost directly inside the
{}and Python will calculate result before building the final string.
Test Cases
Test Case 1 (From Starter Code)
* Input: user_name = "Alice", event_name = "Python Developer Conference", ticket_price = 150.00, ticket_quantity = 3
* Expected Output:
Hello Alice, your 3 tickets for Python Developer Conference are confirmed! Total cost: $450.0.
Test Case 2
* Input: user_name = "Marcus", event_name = "Data Science Expo", ticket_price = 45.50, ticket_quantity = 2
* Expected Output:
Hello Marcus, your 2 tickets for Data Science Expo are confirmed! Total cost: $91.0.