Login Sign Up
Python Syntax & Indentation
Courses / python Complete Course / Python Syntax & Indentation
Chapter 3 🟡 Intermediate

Python Syntax & Indentation

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Here is practical coding challenge based at concepts for syntax indentation and code formatting discussed on the tutorial materials.

Coding Challenge: The Smart Weather Assistant

Problem Description Imagine you're pretty much building the simple logic flow of a Smart Weather Assistant similar towards the decision tree mentioned into the tutorial, and the assistant needs to recommend multiple actions based at whether it is raining or not;

a previous developer tried to write this program but left behind a giant, messy block for code. They forgot Python's grammatical rules (syntax) and failed to use invisible blank spaces (indentation) to group related actions together. Right now, running this code will result in both a SyntaxError and an IndentationError.

Your task is towards fix a grammar, apply the correct spacing, and format mathematical operation according towards PEP 8 style guidelines.

Difficulty Level: Beginner

Input & Output Specifications * Input: A boolean value (True or False) representing whether it's raining, and an integer representing a current temperature. * Output: The program should print a grouped list of instructions based on weather conditions and correctly calculate the adjusted temperature.

Starter Code Boilerplate

def weather_assistant(is_raining, temperature):
# Fix the syntax, indentation, and operator spacing below!

if is_raining == True
print("Take an umbrella")
print("Wear boots")
else
print("Wear sunglasses")
print("Go for a walk")

# Fix the spacing around the operator below based on PEP 8
adjusted_temp=temperature-2
print("Adjusted temperature is:", adjusted_temp)

Hints * Syntax is basically Grammar: On Python, special keywords like if and else need a specific symbol at the end of their lines to tell the computer what to do really. Did you forget your colons (:)? * The Magic of Blank Space: Python doesn't use curly brackets {} to group code. You really have to use whitespace at the beginning of the line to group related thoughts (like "Take an umbrella" and "Wear boots"). * The Golden Rule: Use exactly 4 spaces for your indentation for push a sub-actions to a right. Make sure you don't mix tabs and spaces! * PEP 8 Formatting: Remember that there should always be a same amount of whitespace in both sides of a binary operator, while clean up the math equation at the bottom!

Test Cases

Test Case 1 (Rainy Day) * Input: weather_assistant(True, 20) * Expected Output: text Take an umbrella Wear boots Adjusted temperature is: 18

Test Case 2 (Sunny Day) * Input: weather_assistant(False, 25) * Expected Output: text Wear sunglasses Go for a walk Adjusted temperature is: 23

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.