3

I have a problem I can't seem to get right.

I have 2 numbers, A & B. I need to make a list of A rows with B columns, and have them print out 'R0CO', 'R0C1', etc.

Code:

import sys

A= int(sys.argv[1])

B= int(sys.argv[2])

newlist = []
row = A
col = B
for x in range (0, row): 
  newlist.append(['R0C' + str(x)])

  for y in range(0, col):
    newlist[x].append('R1C' + str(y))

print(newlist)

This is not working. The following is the output I get and the expected output: Program Output

Program Failed for Input: 2 3 Expected Output:

[['R0C0', 'R0C1', 'R0C2'], ['R1C0', 'R1C1', 'R1C2']]

Your Program Output:

[['R0C0', 'R1C0', 'R1C1', 'R1C2'], ['R0C1', 'R1C0', 'R1C1', 'R1C2']]

Your output was incorrect. Try again

4
  • Thank you for editing my question, Patrick. I have only been programming for three weeks, and just joined this site the other day. Still learning. Commented Nov 21, 2017 at 20:32
  • You should be appending in only one place, the innermost for loop. You should also be making the row numbers in the strings programmatically, not hardcoding R0C. Look at this page for some techniques for formatting python strings Commented Nov 21, 2017 at 20:35
  • 1
    @PatrickHaugh They still need to append a new list on each iteration of the outer loop, though Commented Nov 21, 2017 at 20:36
  • @AlexvonBrandenfels Good point. Maybe better to say that the strings should only be added in the innermost loop Commented Nov 21, 2017 at 20:38

3 Answers 3

5

You are first adding R0Cx and then R1Cxy. You need to add RxCy. So try:

newlist = []
row = A
col = B
for x in range (0, row): 
  newlist.append([])

  for y in range(0, col):
    newlist[x].append('R' + str(x) + 'C' + str(y))

print(newlist)
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect!! I was getting close, but just saw this. Can you explain real quick why we use empty brackets for the first append?
It‘s for adding an empty array you can fill in the second loop. Otherwise you would have nothing to fill.
Got it. I figured it out after thinking about it, but thank you for your reply!
0

You have to fill columns in a row while still in that row:

rows = []
row = 2
col = 3

for x in range(0, row):
    columns = []
    for y in range(0, col):
        columns.append('R' + str(x) + 'C' + str(y))

    rows.append(columns)


print(rows)

will print:

[['R0C0', 'R0C1', 'R0C2'], ['R1C0', 'R1C1', 'R1C2']]

Comments

0

Try changing your range commands as shown:

for x in range (row): 
  for y in range(col):

In fact, you have a second issue that you are not modifying the text properly:

newlist = []
row = A
col = B
for x in range (row):   
    sublist = []
    for y in range(col):
        sublist.append('R{}C{}'.format(x, y))
    newlist.append(sublist)

2 Comments

This way is obviously cleaner, but functionally identical.
That's true, the real issue is with the way you are looping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.