After working through this lesson, you’ll be able to
Write Python code using the for loop using the range function with two arguments.
Introductory Problem
Using a for loop, print numbers 0 - 6
intro.py
done = False
while not done:
print("Menu")
print("I - Introductory Problem")
print("Q - Quit")
choice = input("Choice: ")
if choice == "I":
print("Introductory Problem")
for x in range(7):
print(x)
elif choice == "Q":
print("Exiting Game!")
done = True
for loop
With two arguments in the range function, the sequence starts at the first value and ends one before the second argument.
Programmers usexorias a stepper variable.
Examples
Example 1
Use a for loop to print numbers from 1 to 6.
example1.py
done = False
while not done:
print("Menu")
print("E1 - Example 1")
print("Q - Quit")
choice = input("Choice: ")
if choice == "E1":
print("Example 1")
for x in range(1,7):
print(x)
elif choice == "Q":
print("Exiting Game!")
done = True
Example 1 Output
Example 2
Using a for loop, create a table to show the numbers -5 to 5 and their squares. Create a header:
print("Number\tSquared")
example2.py
done = False
while not done:
print("Menu")
print("E2 - Example 2")
print("Q - Quit")
choice = input("Choice: ")
if choice == "E2":
print("Example 2")
print("Number\tSquared")
for x in range(-5,6):
print(x,"\t",x**2)
elif choice == "Q":
print("Exiting Game!")
done = True
Example 2 Output
Example 3
Ask the user for a range, meaning a minimum and maximum, of numbers . Determine how many numbers are divisible by a number within that range. Divisible means capable of being divided by another number without a remainder. How many questions do you need to ask? Hint: use the modulus operator %.
example3.py
done = False
while not done:
print("Menu")
print("E3 - Example 3")
print("Q - Quit")
choice = input("Choice: ")
if choice == "E3":
print("Example 3")
count = 0
min = int(input("Min: "))
max = int(input("Max: "))
divisible = int(input("Divisible: "))
for x in range(min,max+1):
if x % divisible == 0:
count = count + 1
print("Count:",count)
elif choice == "Q":
print("Exiting Game!")
done = True
Example 3 Output
Example output if you choose 5 for the minimum, 10 for the maximum and divisible of 2.
Example 4
From yesterday’s lesson, using a for loop, ask the user for 5 integers. Print the third input.
example4.py
done = False
while not done:
print("Menu")
print("E4 - Example 4")
print("Q - Quit")
choice = input("Choice: ")
if choice == "E4":
for x in range(5):
num = int(input("Number: "))
if x == 2:
third = num
print("Third Number:",third)
elif choice == "Q":
print("Exiting Game!")
done = True
Example 4 Output
Example output if you choose the numbers 10, 3, 4, 11 and 3.
Combined Examples
done = False
while not done:
print("Menu")
print("E1 - Example 1")
print("E2 - Example 2")
print("E3 - Example 3")
print("E4 - Example 4")
print("Q - Quit")
choice = input("Choice: ")
if choice == "E1":
print("Example 1")
for x in range(1,7):
print(x)
elif choice == "E2":
print("Example 2")
print("Number\tSquared")
for x in range(-5,6):
print(x,"\t",x**2)
elif choice == "E3":
print("Example 3")
count = 0
min = int(input("Min: "))
max = int(input("Max: "))
divisible = int(input("Divisible: "))
for x in range(min,max+1):
if x % divisible == 0:
count = count + 1
print("Count:",count)
elif choice == "E4":
for x in range(5):
num = int(input("Number: "))
if x == 2:
third = num
print("Third Number:",third)
elif choice == "Q":
print("Exiting Game!")
done = True