Skip to content

A3 - Using Python's range function

Reference for this assignment

Learning Goal

Understand how to loop effectively using the range function, and begin to apply this skill to creatively solve problems.

Suggested Task Choices

READ THIS FIRST

You do not need to do all of these questions! I asked AI to generate several questions on this topic at several difficulty levels, and I'd like you to pick roughly three that you think match your skill level.

If you're able to do questions at around Level 2 below (even if you also choose something from Level 1), I'd consider that meeting expectations.

🟢 Level 1: Basic Loop Mechanics

  1. Counting up
for i in range(5):
    print(i)

🔹 Exercise: Modify it to start at 1, stop at 10, or count by 2s.

  1. Print a message multiple times

    • “Print ‘Hello!’ 10 times.”
    • “Print your name 5 times with the number beside it.”
  2. Summing numbers

    • “Print the sum of numbers from 1 to 10.”
    • “Print the sum of even numbers from 2 to 20.”
  3. Countdown

    • “Print numbers from 10 down to 1, then print ‘Blastoff!’”

🟡 Level 2: Using range() creatively

Focus: step values, parameters, and controlling repetition.

  1. Odd and even sequences

    • “Print all odd numbers from 1 to 19.”
    • “Print all even numbers from 2 to 20.”
  2. Squares and cubes

    • “Print the square of each number from 1 to 10.”
    • “Print each number and its cube.”
  3. Multiplication tables

    • “Print the 5-times table (from 1×5 to 10×5).”
    • Challenge: “Let the user input a number, and print that multiplication table.”
  4. Running total

    • “Use a loop to calculate the sum of numbers from 1 to 100.”

🟠 Level 3: Nested Loops and Simple Patterns

Focus: using loops to make shapes or grids.

  1. Simple stars

    • “Print 5 stars in a row: *****.”
    • “Print 5 rows of 5 stars (a square).”
  2. Triangle patterns

    *
    **
    ***
    ****
    *****
    

    Challenge: reverse the triangle or make it right-aligned.

  3. Number patterns

    1
    12
    123
    1234
    

    (use print(i, end='') inside the inner loop)


🔵 Level 4: Combining Loops with Logic

Focus: integrating conditions inside loops.

  1. FizzBuzz

    • “For numbers 1 to 20, print ‘Fizz’ for multiples of 3, ‘Buzz’ for multiples of 5, and ‘FizzBuzz’ for both.”
  2. Skip certain numbers

    • “Print numbers from 1 to 20, but skip 13.”
  3. Countdown timer

    • “Print numbers from 5 to 1 with a 1-second pause between each (hint: use time.sleep(1)).”

🟣 Level 5: Mini Projects

Focus: synthesis and creativity.

Additional Information for Level 5

Some of these programs will require you to pause the program and/or clear the screen. Here is an example of how you use both of these features:

# Required imports
import time
import os

os.system('cls') # Clear screen
for i in range(1, 11): # Count 1 to 10

    for j in range(1, i+1): # Count from 1 to the current i value
        print(j, end=" ") # Print j followed by a space
    print("") # This is required or the previous line will not show up since it doesn't end in a newline character
    time.sleep(1) # Pause 1 second
    os.system('cls') # Clear the screen
  1. Average of numbers

    • “Ask the user for 5 test scores, then print the average.”
  2. Simple animation (text-based)

    • “Print a dot that moves across the screen one step per loop.”
  3. ASCII progress bar

    [#####     ] 50%
    
    • “Print a growing bar in 10 steps.”