+ 2
What did I do wrong?
I was doing an very easy exercise and wrote such code: for n in range(int(input())): print(n if n % 5 == 0) But sololearn’s playground says ‘invalid syntax: line 2’, I think u’ll understand what should this program do, what did I do wrong?
5 ответов
+ 2
That's not a valid syntax .
Correct way,
for n in range(25):
print(n) if n%5 == 0 else ""
+ 2
TriXioN
You can do so inside the print as well, but you need the else statement also. There are also going to be formatting differences between the 2 variations as 1 will always print() so the outputs will be spaced out by several lines, while the other will not. Try running both to see the differences.
for i in range(25):
print(i if i%5==0 else '')
Vs
for i n range(25):
print(i) if i%5==0 else None
+ 1
Abhay
No, print() function can take ‘if’ condition, it works in my code
https://sololearn.com/coach/1042/?ref=app
0
Abhay
But why we should to put *if* out of brackets? It worked in brackets before
0
TriXioN probably because print is a function that takes an argument and just prints it . It isn't going to evaluate the if condition like we do in list comprehension .