Nested While Loops

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.

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()

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

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

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:

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

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

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?

Apologies - no. 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:

for i in range(num_rows):

Are you familiar with the documentation on the official Python web site? See Built-in Functions. The following may be particularly helpful for this project.

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.

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

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 for advice on how to do this. :smiley:

copied sir … :smiley: will do better next time

1 Like