+ 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?

24th Sep 2021, 11:04 PM
TriXioN
TriXioN - avatar
5 ответов
+ 2
That's not a valid syntax . Correct way, for n in range(25): print(n) if n%5 == 0 else ""
24th Sep 2021, 11:12 PM
Abhay
Abhay - avatar
+ 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
25th Sep 2021, 12:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Abhay No, print() function can take ‘if’ condition, it works in my code https://sololearn.com/coach/1042/?ref=app
24th Sep 2021, 11:31 PM
TriXioN
TriXioN - avatar
0
Abhay But why we should to put *if* out of brackets? It worked in brackets before
24th Sep 2021, 11:18 PM
TriXioN
TriXioN - avatar
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 .
24th Sep 2021, 11:21 PM
Abhay
Abhay - avatar