# Nested While Loops

**URL:** <https://discuss.python.org/t/nested-while-loops/11370>\
**Category:** Python Help\
**Created:** [October 20, 2021, 9:31pm UTC](https://discuss.python.org/t/nested-while-loops/11370 "2021-10-20T21:31:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 20, 2021, 9:31pm UTC](https://discuss.python.org/t/nested-while-loops/11370/1 "2021-10-20T21:31:32Z")

</div>

The following program is supposed to output numbered rows and columns (as in 1A, 1B, 1C, etc.) based on user input. If user enters 1 3, the output should be (1A 1B 1C). If the user enters 5C, the output should be (1A 1B 1C 2A 2B 2C 3A 3B 3C 4A 4B 4C 5A 5B 5C), etc.

I’m struggling with lines 9 and 10 of the code. I know that somewhere in there I need to include `num_cols` but so far I’m having a really hard time figuring out where it should go.

If I assign `j = "A"` then J becomes a string and I can’t compare it to num\_cols, but if I don’t assign `j = "A"` then I’m not sure how to get the j to spit out alpha characters over numeric. Any help is greatly appreciated.

```auto
num_rows = int(input())
num_cols = int(input())

# Note 1: You will need to declare more variables
# Note 2: Place end=' ' at the end of your print statement to separate seats by spaces
i = 1
j = 1
while i <= num_rows:
    j = "A"
    while j <= "C":
        print("{}{}".format(i, j), end= " ")
        j = chr(ord(j) + 1)
    i += 1
print()

```

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 20, 2021, 9:55pm UTC](https://discuss.python.org/t/nested-while-loops/11370/2 "2021-10-20T21:55:33Z")

</div>

Rather than while loop (where you would later have to “increment” your string to the next letter), why not make it a for loop?

```python
from string import ascii_uppercase
...
    for j in ascii_uppercase[:num_cols]
...

```

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 20, 2021, 10:00pm UTC](https://discuss.python.org/t/nested-while-loops/11370/3 "2021-10-20T22:00:15Z")

</div>

> [@Rohagan4](#):
>
> ```auto
> num_rows = int(input())
> num_cols = int(input())
> 
> # Note 1: You will need to declare more variables
> # Note 2: Place end=' ' at the end of your print statement to separate seats by spaces
> i = 1
> j = 1
> while i <= num_rows:
> j = "A"
> while j <= "C":
> print("{}{}".format(i, j), end= " ")
> j = chr(ord(j) + 1)
> i += 1
> 
> ```

I’m using an online textbook so I’m not sure how libraries/modules work on it. I’m getting the following error when I try the code your way:

 ![Screen Shot 2021-10-20 at 5.59.36 PM](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/e/eb52a48ecbd3296ac2c1b44584a64e497edc1962.png)

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 20, 2021, 10:41pm UTC](https://discuss.python.org/t/nested-while-loops/11370/4 "2021-10-20T22:41:41Z")

</div>

is the `<=` string in your code? That shouldn’t be there.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [October 20, 2021, 10:52pm UTC](https://discuss.python.org/t/nested-while-loops/11370/5 "2021-10-20T22:52:05Z")

</div>

Please cut/paste the full error text inline. Those of us on email do not  
see screenshots, and even if we visit the forum we can’t cut/paste from  
the text in a screenshot (leaving aside whether any vision impairment  
might make the font/colour in the screenshot hard to work with).

Thanks,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [October 20, 2021, 11:43pm UTC](https://discuss.python.org/t/nested-while-loops/11370/6 "2021-10-20T23:43:03Z")

</div>

> [@Rohagan4](#):
>
> If user enters 1 3, the output should be (1A 1B 1C). If the user enters 5C, the output should be (1A 1B 1C 2A 2B 2C 3A 3B 3C 4A 4B 4C 5A 5B 5C), etc.

Is it correct, as the above suggests, that the user may enter either a number or an uppercase letter to specify the number of columns?

---

<div class="post-metadata">

**Author:** ![Rohagan4](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@Rohagan4](https://discuss.python.org/u/Rohagan4)\
**Post date:** [October 20, 2021, 11:54pm UTC](https://discuss.python.org/t/nested-while-loops/11370/7 "2021-10-20T23:54:01Z")

</div>

Apologies - no. The user may only enter int.

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [October 21, 2021, 12:03am UTC](https://discuss.python.org/t/nested-while-loops/11370/8 "2021-10-21T00:03:43Z")

</div>

> [@Rohagan4](#):
>
> The user may only enter int.

Thanks. You can use nested `for` loops controlled by the user’s input.

Here is a suggested header for the outer loop:

```auto
for i in range(num_rows):

```

Are you familiar with the documentation on the official [Python](https://www.python.org/) web site? See [Built-in Functions](https://docs.python.org/3/library/functions.html). The following may be particularly helpful for this project.

- [`chr()`](https://docs.python.org/3/library/functions.html#chr)
- [`ord()`](https://docs.python.org/3/library/functions.html#ord)

Those functions may help you translate between numbers and letters. That can be useful for handling the columns.

_EDITED on October 20, 2021 to add suggestions._

---

<div class="post-metadata">

**Author:** ![DEVANS](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/devans/32/12269_2.png) [@DEVANS](https://discuss.python.org/u/DEVANS)\
**Post date:** [October 21, 2021, 8:11am UTC](https://discuss.python.org/t/nested-while-loops/11370/9 "2021-10-21T08:11:53Z")

</div>

num\_rows = int(input(" Ryan O’Hagan please enter your number \n"))  
i = 1  
j = ()  
while i \<= num\_rows:  
j = “A”  
while j \<= “C”:  
print("{}{}".format(i, j), end= " ")  
j = chr(ord(j) + 1)  
i += 1  
print()

@Rohagan4#Check this out Ryan. You declared a global variable j with a value and also declared local variable j with different value. “instead of making j = 1 in global var, do it j =()” Also I suppose there is no need for num\_cols… Please Run and see if that is how you want it

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [October 21, 2021, 11:02am UTC](https://discuss.python.org/t/nested-while-loops/11370/11 "2021-10-21T11:02:36Z")

</div>

Hello, all!

It is a good idea to format Python code for posting. Then it will be properly indented, and therefore easy for other users to copy and paste for testing. Please see [About the Users category](https://discuss.python.org/t/about-the-users-category/42) for advice on how to do this. 😃

---

<div class="post-metadata">

**Author:** ![DEVANS](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/devans/32/12269_2.png) [@DEVANS](https://discuss.python.org/u/DEVANS)\
**Post date:** [October 23, 2021, 1:10am UTC](https://discuss.python.org/t/nested-while-loops/11370/12 "2021-10-23T01:10:25Z")

</div>

copied sir … 😃 will do better next time
