Frage

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.

War es hilfreich?

Lösung

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])"

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top