← Introduction to Programming

Problem 1: The Movie Ticket Pricer
  • Problem Statement: You are building a pricing system for a movie theater. The price of a ticket depends on the customer’s age. Write a program that asks the user for their age and then prints the corresponding ticket price based on the following rules:
    • Children (age 12 and under): $8
    • Adults (age 13 to 64): $15
    • Seniors (age 65 and over): $10
  • Key Concepts to Apply:
    • Getting user input with input().
    • Converting the input string to a number with int().
    • Using an if-elif-else statement to handle multiple conditions.
  • Hint: Think about the order of your conditions. You can check for the youngest group first (age <= 12), then the oldest (age >= 65), and the else block can handle everyone in between.

  • Example Output (for an adult):
    --- Movie Ticket Pricer ---
    Please enter your age: 35
    You fall into the 'Adult' category.
    Your ticket price is: $15
    

Problem 2: The Running Total Calculator
  • Problem Statement: Write a program that repeatedly asks the user to enter a number. The program should add each number to a running total. When the user enters the word “done”, the loop should stop, and the program should print the final total.

  • Key Concepts to Apply:
    • Using a while loop to repeat an action an unknown number of times.
    • Initializing an “accumulator” variable (like total) to zero before the loop.
    • Using an if statement inside the loop to check for the stop condition (“done”).
    • Using break to exit the loop.
    • Converting input to a number with float() to allow for decimals.
  • Hint: A while True: loop is a great pattern for this. Inside the loop, get the user’s input. Check first if the input is “done”. If it is, break. If it’s not, convert it to a number and add it to your total.

  • Example Output:
    --- Running Total Calculator ---
    Enter numbers to add them up. Type 'done' to see the total.
    Enter a number or 'done': 10
    Current total: 10.0
    Enter a number or 'done': 5.5
    Current total: 15.5
    Enter a number or 'done': 20
    Current total: 35.5
    Enter a number or 'done': done
    
    --- Final Calculation ---
    The final sum of all numbers is: 35.5
    

Problem 3: Simple Triangle Pattern
  • Problem Statement: Write a program that asks the user for an integer representing the height of a triangle. Then, use nested loops to print a right-angled triangle of that height using asterisks (*).

  • Key Concepts to Apply:
    • Nested for loops.
    • Using the outer loop’s variable to control the inner loop’s range().
    • Using print("*", end="") to print without moving to a new line.
    • Using an empty print() to move to the next line.
  • Hint: The outer loop will control the rows (from 1 up to the height). For each row, the inner loop will control how many asterisks to print. For row 1, print 1 asterisk. For row 2, print 2 asterisks, and so on. The number of columns in each row is equal to the current row number.

  • Example Output (for a height of 5):

    --- Triangle Pattern Printer ---
    Enter the desired height of the triangle: 5
    * 
    * * 
    * * * 
    * * * * 
    * * * * * 
    

Problem 4 (Advanced): The "FizzBuzz" Challenge
  • Problem Statement: This is a classic programming interview question. Write a program that prints the numbers from 1 to 100, each on a new line. However, for multiples of three, print “Fizz” instead of the number. For multiples of five, print “Buzz”. For numbers which are multiples of both three and five, print “FizzBuzz”.

  • Key Concepts to Apply:
    • A for loop using range(1, 101).
    • The modulo operator (%) to check for divisibility.
    • A carefully ordered if-elif-else statement. The order matters!
  • Hint: The most important part of this problem is the order of your checks. If you check for divisibility by 3 before you check for divisibility by both 3 and 5, you will never print “FizzBuzz”. The most specific condition (number % 3 == 0 and number % 5 == 0) must be checked first.

  • Example Output (showing the first 16 lines):
    --- FizzBuzz Challenge (1-100) ---
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    16
    ...
    

Problem 5 (Advanced): The Fibonacci Sequence Generator
  • Problem Statement: The Fibonacci sequence is a famous mathematical sequence where each number is the sum of the two preceding ones. It starts with 0 and 1. The sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... Write a program that asks the user for a quantity (e.g., “how many numbers?”) and then generates and prints that many numbers from the Fibonacci sequence.

  • Key Concepts to Apply:
    • Getting user input and converting it to an int.
    • Using a for loop to iterate a specific number of times.
    • Managing and updating multiple variables inside a loop to keep track of the state (the previous two numbers).
  • Hint: You’ll need two variables to store the two previous numbers in the sequence, let’s call them a and b. Start them at 0 and 1. Inside your loop, you will:
    1. Print the current value of a.
    2. Calculate the next number in the sequence (a + b).
    3. Update a and b for the next iteration. The old b becomes the new a, and the calculated sum becomes the new b. A clever way to do this in Python is with simultaneous assignment: a, b = b, a + b.
  • Example Output:
    --- Fibonacci Sequence Generator ---
    How many Fibonacci numbers would you like to generate? 12
    0 1 1 2 3 5 8 13 21 34 55 89