Login Sign Up
Challenges / python 3.10 match case structural pattern matching 2024

python 3.10 match case structural pattern matching 2024 Challenge

Read the problem description and solve the challenge in the workspace.

Open Full Sandbox Studio
Problem Description

While there is no specific document titled "Practice Quiz on Python Match-Case (3.10+)" included into an uploaded sources I have drawn extensively from the "Tutorial on Python Match-Case (3.10+)" to create this practical coding challenge perfectly tailored to the structural pattern matching concepts taught.

Here is your coding challenge:


Coding Challenge: The Automated Customer Service Chatbot

Problem Description

You have been hired to program the backend routing logic for new automated customer service chatbot. When user types the specific command into the chat window the system needs to instantly sort that command and respond with exact right automated message.

Instead for writing a long, messy chain of if-elif-else statements, you will build a smart sorting funnel using Python 3.10's modern match-case structural pattern matching. Your program will check a structure of the user's input and slide it into the correct "case" bin to generate a proper response, ensuring there is actually the safe fallback if the user types something a bot doesn't understand.

Difficulty Level

Beginner

Input Specifications

You'll just be provided with the following predefined variable: * user_command (string): The text command typed by the user (e.g., "Help", "Order Status", "Return Item").

Output Specifications

Your code must evaluate the command and assign a correct string to a variable called bot_response: 1. If the command is "Help", the response should be: "How can I assist you today?" 2; if the command is "Order Status", response should be: "Please provide your order number." 3. If command is "Return Item" response should be: "Let's get your return started." 4, and if the command is basically anything else, the response should be: "I'm sorry, I didn't understand that command."


Starter Code Boilerplate
# The Automated Customer Service Chatbot

user_command = "Order Status" 
bot_response = ""

# Write your match-case logic below this line:



# Print the final output
print("Chatbot:", bot_response)

Hints
  • The Match Keyword: Start your block by passing the variable you want to inspect into the match keyword (e.g., match user_command:).
  • Sorting Bins: Use the case keyword followed by the specific string you're checking for (e.g., case "Help":) to create your sorting bins. Make sure to use colon : on an end of each case line and indent code underneath it!
  • A Safety Net: Don't forget Python's wildcard! Use case _: at the very bottom of your match block. This acts exactly like the else statement and will catch any unexpected commands (like if a user types "Pizza") so your program doesn't break.

Test Cases

Test Case 1 (From Starter Code) * Input: user_command = "Order Status" * Expected Output: Chatbot: Please provide your order number. * (Explanation: Python looks at the variable and instantly finds the exact case that matches "Order Status", skipping all other checks.)

Test Case 2 (Return Request) * Input: user_command = "Return Item" * Expected Output: Chatbot: Let's get your return started.

Test Case 3 (Unrecognized Command) * Input: user_command = "Pizza" * Expected Output: Chatbot: I'm sorry, I didn't understand that command. * (Explanation: Because there's basically no specific case built for "Pizza", the program safely falls back to the case _: wildcard and outputs the default error message.)

Loading sandbox workspace environment...

Verify Your Solution

Run assertions against your code in the sandbox environment.

Sandbox Instructions

1. Click Copy Starter Boilerplate at the top to copy function definition.
2. Use the interactive compiler to implement and run your code securely.
3. Click Verify & Submit Solution to validate your code.