Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested Loop functions Python

Given num_rows and num_cols, output the label for each seat of a theater. Each seat is followed by a space, and each row is followed by a newline. Define the outer for loop, initialize curr_col_let with the starting row letter, and define the inner for loop.

I have seen this question asked before, but I did not see a usable solution for what I have to work with. Any advice would be greatly appreciated.

num_rows = int(input()) #line locked and can not be altered.
num_cols = int(input()) #line locked and can not be altered.

curr_row = 1
curr_col_let = 'A'
for row in range(num_rows):
  for col in range(num_cols):
      print (f'{curr_row}{curr_col_let}', end=' ')  #line locked and can not be altered.
      curr_col_let = chr(ord(curr_col_let) + 1)  #line locked and can not be altered.
  print() #line locked and can not be altered.

Current Output:
current output and expected

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I wanted to add the row + 1 after the print on line 8, but Zylab will not let me alter lines 1, 2, 8, 9, 10. I can enter new lines between line 2 and the first print function. Struggling to figure out what function can be put in above the print function to increase the row after the column iterations.

>Solution :

Remove the curr_row = 1 and change the outermost for-loop with curr_row variable (and adjust the range() accordingly).

Also reset the curr_col_let to A every new row.

num_rows = int(input())  # line locked and can not be altered.
num_cols = int(input())  # line locked and can not be altered.

for curr_row in range(1, num_rows + 1):
    curr_col_let = "A"
    for _ in range(num_cols):
        print(f"{curr_row}{curr_col_let}", end=" ")
        curr_col_let = chr(ord(curr_col_let) + 1)
    print()

Prints:

2
3
1A 1B 1C 
2A 2B 2C 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading