Naushad Warsi
Naushad Warsi

Reputation: 89

syntax error in print statement but syntaticly code is correct

If I run this code:

x, y, z, n = (int(input() for i in range(4))
print ([[a,b,c] for a in range(0,x+1)
                for b in range(0,y+1) 
                for c in range(0,z+1) if a + b + c != x])

I am getting syntax error in print statement(2nd line),

but if I run this code:

x, y, z, n = int(input()), int(input()), int(input()), int(input())
print ([[a,b,c] for a in range(0,x+1) 
                for b in range(0,y+1) 
                for c in range(0,z+1) if a + b + c != n ])

it is running without error.

can't understand what I am missing with the syntax, please help

Upvotes: 0

Views: 196

Answers (1)

Baltasarq
Baltasarq

Reputation: 12202

You need to correctly balance parenthesis and square brackets in your first line (the "offending" example):

x, y, z, n = [int(input()) for i in range(4)]

This is a list comprehension, so you need to put it inside []. Also, since you want integer values in x, y, z and n, you are correctly using the int() constructor, but failing to close the parenthesis after input().

With that corrected, it works without a glitch.

Hope this helps.

Upvotes: 1

Related Questions