문제

I am trying to input a string using echo into a Python one liner then perform a Caeasar's Cipher on the string.

One of the examples my instructor gave me was this.

~ $ echo "Hello Holly." | python -c "import sys; [print(line) for line in sys.stdin]"

The output is suppose to be: Hello Holly.

How ever when I type the command in I get:

File "<string>", line 1
 import sys; [print(line) for line in sys.stdin]
                  ^
SyntaxError: invalid syntax

I would appreciate it if someone could point out the error to me. I am using Python 2.6 on Centos 6.

Thanks.

도움이 되었습니까?

해결책

In Python 2 print is a statement, not a function. Try this instead:

echo "Hello Holly." | python -c "import sys; print [line for line in sys.stdin]"

Alternatively, you could use the following to just print plain text (thanks @mgilson):

echo "Hello Holly." | python -c "import sys; print ' '.join([line for line in sys.stdin])"

다른 팁

It looks like you are using python2 while your isntructor is using python 3.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top