# 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.

{% embed url="<https://docs.google.com/presentation/d/1AveaRWfw_gFcn-pLx33Wiw9HOFfon5pJtx-cGOwJGPk/edit?usp=sharing>" %}

## Introductory Problem

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

{% code title="menu.py" %}

```python
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
```

{% endcode %}

{% hint style="info" %}
done is a Boolean variable and True is the sentinel value.
{% endhint %}

Add code for the Intro menu item when the user selects I. &#x20;

Using a while loop, print your name 5 times. &#x20;

{% code title="intro.py" %}

```python
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
```

{% endcode %}

## 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.

![](/files/-Ld1TFkOIEArjlGIZDcn)

{% hint style="info" %}
Programmers us&#x65;**`x`**&#x6F;&#x72;**`i`**&#x61;s a stepper variable.
{% endhint %}

![](/files/-Ltvzdp9BHZsEci0pMfn)

## Examples

### Example 1

Add a Menu Item E1 - Example 1

Using a for loop, print your name 5 times.

{% code title="example1.py" %}

```python
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)
```

{% endcode %}

### Example 2

Add a Menu Item E2 - Example 2

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

{% code title="example2.py" %}

```python
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)
```

{% endcode %}

### 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.

{% code title="example3.py" %}

```python
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)
```

{% endcode %}

### 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")

{% code title="example4.py" %}

```python
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)
```

{% endcode %}

## Conclusion

* In your own words, what is a for loop?
* What are the differences between a while loop and a for loop.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pythonclassroom.com/loops/for-loop-range-one-argument.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
