Skip to content

A5 - Using the range function

Group Activity (Introduction)

In the groups that you will be assigned, you will copy the following code into a file and run it:

print("Result of first loop:")
for i in range(6):
    print(i)

print("Result of second loop:")
for i in range(3, 8):
    print(i)

print("Result of third loop:")
for i in range(1, 12, 2):
    print(i)

Try to determine how range will behave when given:

  • one argument
  • two arguments
  • three arguments

Feel free to experiment with the code a bit until you're confident you've figured out the pattern.


More Information Is Here - After we're done the group activity, this would be a good link to refer to if you forget exactly how these work.

Task Choices

READ THIS FIRST

You do not need to do all of these question! Pick roughly three of these that you think match your skill level.

Disclaimer: I used AI to generate these questions. As always, if you use AI to help you come up with an answer, make sure you fully understand and are able to explain through the solution you are submitting.

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

Focus: understanding the loop counter and repetition.

  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. Guess-the-number helper

    • “Print a list of possible guesses (1–100), narrowing by halves.”
  4. ASCII progress bar

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