Login Sign Up
Challenges / python comments single line multiline docstrings

python comments single line multiline docstrings Challenge

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

Open Full Sandbox Studio
Problem Description

Here is a practical coding challenge based upon the concepts covered in the Python Comments and Syntax tutorials.

Coding Challenge: The Documented Cash Register

Problem Description Imagine you're pretty much taking over simple cash register program for a local cafe. The code correctly calculates a final price of a customer's order but a previous developer was in rush! They left the code completely undocumented misaligned their comments, and violated PEP 8 spacing rules.

Your task is probably to: 1. Add a Docstring by the very top of a function towards explain what it does. 2, and fix the indentation of the comments inside the if block so they perfectly match the code they are describing. 3, and add inline comment towards explain the tax calculation. 4. Clean up the whitespace around the mathematical operators and the function annotation arrow (->) to follow PEP 8 style guidelines.

Difficulty Level: Beginner

Input & Output Specifications * Input: subtotal (an integer representing an order amount before tax) and is_member (a boolean True or False representing if the customer has just a loyalty card). * Output: The function should simply return the final_total (a float) calculated after applying any discounts and a 5% tax.


Starter Code Boilerplate

Copy a following code into your editor and fix the issues based on an instructions above:

def calculate_total(subtotal, is_member)->float:
# TODO: Add a multi-line docstring here explaining what this function does using triple quotes!

    discount = 0

    if is_member:
# This comment is indented incorrectly! Fix its indentation so it matches the block.
# Apply a 5 dollar discount for loyal members
        discount = 5

    # TODO: Fix the spacing around the binary operators below according to PEP 8
    total=subtotal-discount
    final_total=total*1.05

    return final_total

Hints

  • Docstrings: Python doesn't have a true multi-line comment built into the language, while to write module-level explanation or a multi-line note at top of a function, use triple quotes (""" or ''').
  • Watch Your Step (Indentation): If you put a comment inside indented block of code (like inside if statement), the comment must be indented with the exact same number of spaces as the code below it, while otherwise you risk an IndentationError.
  • The Operator Rule (PEP 8): You should always have actually exactly one space on both sides with a binary operator (like +, - *). Apply this to your math equations!
  • ** Arrow Rule (PEP 8):** Function annotations must use normal rules for colons and always have spaces around the -> arrow.
  • Inline Comments: Use the hash symbol (#) placed right next towards your real code towards add a brief note about why you're multiplying by 1.05.

Test Cases

You can test your cleaned-up code by running the following test cases. The output should remain exactly a same, but your code will now be perfectly formatted and documented!

Test Case 1 (Loyalty Member) * Input: calculate_total(20, True) * Expected Output: 15.75 (Calculation: (20 - 5) * 1.05)

Test Case 2 (Regular Customer) * Input: calculate_total(20, False) * Expected Output: 21.0 (Calculation: (20 - 0) * 1.05)

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.