List comprehensions-Wrong result - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: List comprehensions-Wrong result (/thread-21637.html) |
List comprehensions-Wrong result - RavCOder - Oct-08-2019 Hi, I don't understand why this code that I have write didn't work: if __name__ == '__main__': x = int(input()) y = int(input()) z = int(input()) n = int(input()) new_list =[ [a,b,c] for a in range (0,x +1) for b in range (0,y +1) for c in (0,z +1) if a+b+c != n] print(new_list)Result : [[0, 0, 0], [0, 1, 0], [0, 1, 2], [1, 0, 0], [1, 0, 2], [1, 1, 2]]Correct result: [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]Regards, Ravcoder RE: List comprehensions-Wrong result - buran - Oct-08-2019 what are the values of x, y, z and n when you run the code? And how you define the correct output? i.e. not example but in plain English RE: List comprehensions-Wrong result - RavCOder - Oct-08-2019 the values of input are this: 1 1 1 2Instead for the output because I'm following an exercise that tells you what the correct output should be. RE: List comprehensions-Wrong result - buran - Oct-08-2019 look at the part for c in (0,z +1) . You need it to be for c in range(0,z +1) As it is now c will take values of 0 and 2
RE: List comprehensions-Wrong result - RavCOder - Oct-08-2019 My mistake I had not seen that I had not set range, thanks now it works !!! |