Skip to main content

Exercices for Python Syntax and Control Structures

Exercise Set 1: Basic Structure and Indentation

  1. Fix the Syntax Look at this code and fix all syntax errors:

    def calculate_total price, tax
    result = price + (price * tax)
    return result

    print(calculate_total(100, 0.2)
  2. Indentation Challenge Properly indent this code to make it work:

    def check_temperature(temp):
    if temp > 30:
    print("It's hot!")
    if temp > 35:
    print("It's very hot!")
    else:
    print("It's warm")
    else:
    print("It's not hot")

Challenge Exercises

  1. Command Line Menu System Create a program that:

    • Shows a menu of options
    • Uses a while loop for continuous operation
    • Has properly indented nested structures
    • Includes functions for each operation
    • Handles invalid inputs
    • Allows user to exit
    • Uses clear, consistent formatting
  2. Game Score Tracker Create a program that:

    • Tracks scores for multiple players
    • Has functions for:
      • Adding players
      • Recording scores
      • Showing rankings
      • Updating scores
    • Uses proper indentation throughout
    • Includes input validation
    • Handles ties appropriately
    • Uses appropriate data structures

Tips for All Exercises:

  • Pay attention to indentation (4 spaces or 1 tab)
  • Use consistent spacing around operators
  • Include appropriate comments