Python Forum

Full Version: List comprehensions-Wrong result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
the values of input are this:
1
1
1
2
Instead for the output because I'm following an exercise that tells you what the correct output should be.
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
My mistake I had not seen that I had not set range, thanks now it works !!!