A3 - Using Python's range function
Reference for this assignment
- Python Manual - Page 6 (bottom half)
- W3Schools Page on this topic
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
- Counting up
🔹 Exercise: Modify it to start at 1, stop at 10, or count by 2s.
-
Print a message multiple times
- “Print ‘Hello!’ 10 times.”
- “Print your name 5 times with the number beside it.”
-
Summing numbers
- “Print the sum of numbers from 1 to 10.”
- “Print the sum of even numbers from 2 to 20.”
-
Countdown
- “Print numbers from 10 down to 1, then print ‘Blastoff!’”
🟡 Level 2: Using range() creatively
Focus: step values, parameters, and controlling repetition.
-
Odd and even sequences
- “Print all odd numbers from 1 to 19.”
- “Print all even numbers from 2 to 20.”
-
Squares and cubes
- “Print the square of each number from 1 to 10.”
- “Print each number and its cube.”
-
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.”
-
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.
-
Simple stars
- “Print 5 stars in a row:
*****.” - “Print 5 rows of 5 stars (a square).”
- “Print 5 stars in a row:
-
Triangle patterns
Challenge: reverse the triangle or make it right-aligned.
-
Number patterns
(use
print(i, end='')inside the inner loop)
🔵 Level 4: Combining Loops with Logic
Focus: integrating conditions inside loops.
-
FizzBuzz
- “For numbers 1 to 20, print ‘Fizz’ for multiples of 3, ‘Buzz’ for multiples of 5, and ‘FizzBuzz’ for both.”
-
Skip certain numbers
- “Print numbers from 1 to 20, but skip 13.”
-
Countdown timer
- “Print numbers from 5 to 1 with a 1-second pause between each (hint: use
time.sleep(1)).”
- “Print numbers from 5 to 1 with a 1-second pause between each (hint: use
🟣 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
-
Average of numbers
- “Ask the user for 5 test scores, then print the average.”
-
Simple animation (text-based)
- “Print a dot that moves across the screen one step per loop.”
-
ASCII progress bar
- “Print a growing bar in 10 steps.”