-2
if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
while True:
    print('[', end="")
    for i in range(0,x+1):
        for j in range(0,y+1):
            for k in range(0,z+1):
                array=[i,j,k]
                if (i+j+k)!=n:
                    print(array, end=", ")

    print(']')
    break

It displays output as: [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1], ]

Required output: [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

How can I delete the last comma?

I have tried using rstrip function inside the loop but it deletes every other comma resulting in answer as: [[0, 0, 0] [0, 0, 1] [0, 1, 0] [1, 0, 0] [1, 1, 1]]

  • You could check the counters when you're in the last iteration and skip the comma, or save the sub-lists and use `join` in the end. OR, just save the whole thing as a 2D-array and print it... – Tomerikoo Apr 05 '20 at 21:21
  • What purpose does the `while True` serve? – Tomerikoo Apr 05 '20 at 21:23
  • What are you trying to do? What is the input? Looks like you just want to call `str()` on a nested list? – mkrieger1 Apr 05 '20 at 21:25

1 Answers1

2

I don't understand what purpose does the while True serve but I simply advise you to use a list and then print it as it is:

while True:
    myList = list()
    for i in range(0,x+1):
        for j in range(0,y+1):
            for k in range(0,z+1):
                array=[i,j,k]
                myList.append(array)

    print(myList)
    break
Amin Guermazi
  • 1,632
  • 9
  • 19