Ask coding questions

← Back to all posts
Invalid Syntax Error
IntellectualGuy (823)

So I'm making a small program that gets user input, and prints it back out, but ignores the letter c(lowercase and uppercase)

Note: Language is python

So I used the following code

for x in input('Enter your phrase: '):print(x,end="") if x.upper() != 'C'

But when I try to run it all I'm getting is Syntax Error: invalid syntax

Why isn't it working?

Answered by Coder100 (18185) [earned 5 cycles]
View Answer
Coder100 (18185)

ur doing the list comprehension syntax a bit wrong

print(''.join('' if x.upper() == 'C' else x for x in input('Enter your phrase: ')))

it goes after the expression

Input: Coder100
Output: oder100


IntellectualGuy (823)

Thanks, but I already found another solution

for x in input('Enter your phrase: '): print(x,end="") if x != 'C' else print('',end="")
Coder100 (18185)

@IntellectualGuy nice

i still think mine is faster, yours has to print 'n' times while mine only has to print once.

IntellectualGuy (823)

@Coder100 Thanks though, I had a competition today, and I used the .join method.