for loop - range (one argument)

Objective

After working through this lesson, you’ll be able to

  • Write Python code using the for loop using the range function with one argument.

Introductory Problem

You worked on while loop menu code previously. Run the code below and make sure you understand how it works.

menu.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Start")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True

done is a Boolean variable and True is the sentinel value.

Add code for the Intro menu item when the user selects I.

Using a while loop, print your name 5 times.

intro.py
done = False
while not done:
    print("Menu")
    print("I - Intro")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "I":
        print("Introductory Problem")
        x = 0
        name = input("What's your name? ")
        while x < 5:
            print(name)
    elif choice == "Q":
        print("Exiting Menu!")
        done = True

for loop

Previously, you used while loops to iterate over a range of integer values. Now you will use a for loop with the range function with one argument.

Programmers usexorias a stepper variable.

Examples

Example 1

Add a Menu Item E1 - Example 1

Using a for loop, print your name 5 times.

example1.py
done = False
while not done:
    print("Menu")
    print("I - Intro")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "I":
        print("Introductory Problem")
        x = 0
        name = input("What's your name? ")
        while x < 5:
            print(name)
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E1":
        print("Example 1")
        name = input("What is your name? ")
        for i in range(5):
            print(name)

Example 2

Add a Menu Item E2 - Example 2

Using a for loop, print numbers from 0 to 4.

example2.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("E2 - Example 2")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E2":
        print("Example 2")
        for i in range(5):
            print(i)

Example 3

Add a Menu Item E3 - Example 3

Using a for loop, ask the user for 3 numbers. Print the sum of numbers.

Initialize the variable sum to 0.

example3.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("E3 - Example 3")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E3":
        print("Example 3")
        sum = 0
        for i in range(3):
            num = int(input("Number: "))
            sum = sum + num
        print(sum)

Example 4

Add a Menu Item E4 - Example 4

Using a for loop, create the table to show the numbers 0 to 10 and their squares. Create a header:

print("Number\tSquared")

example4.py
done = False
while not done:
    print("Menu")
    print("S - Start")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Starting Game!")
    elif choice == "Q":
        print("Exiting Menu!")
        done = True
    elif choice == "E4":
        print("Number\tSquared")
        for i in range(11):
            print(i,"\t",i**2)

Conclusion

  • In your own words, what is a for loop?

  • What are the differences between a while loop and a for loop.

Last updated