0

I wanted to read a variable from terminal by running a script. This is my script.py:

while True:
  value = input('enter text: ')
  if value == 'stop':
    print('bye-bye')
    break
  else:
    print('continue!')

However, when I ran python script.py, something weird happened. If i entered int-data (for instance 1,2,3), there was no problem. If i entered 'stop', I just got an error:

SyntaxError: invalid syntax
DavidG
  • 24,279
  • 14
  • 89
  • 82
Harry
  • 77
  • 8
  • i run this, and it works with `stop` – Druta Ruslan Jun 21 '18 at 13:54
  • What is the full error traceback? – DavidG Jun 21 '18 at 13:55
  • 6
    Possible duplicate of [Command-line input causes SyntaxError](https://stackoverflow.com/questions/2589309/command-line-input-causes-syntaxerror) ---- You're running python 2 so you need `raw_input`. This is python 3 code, where `raw_input` was replaced by `input`. – FHTMitchell Jun 21 '18 at 13:55
  • Thank you, but really? i can't. Do you know how to post a picture in the comment? – Harry Jun 21 '18 at 13:55
  • input text:'stop' Traceback (most recent call last): File "test.py", line 11, in value = input('input text:') File "", line 1 'stop' ^ – Harry Jun 21 '18 at 13:56
  • Which version of python are you using ? – Thibault D. Jun 21 '18 at 13:57
  • @harry you can edit your question to add new information. Don't post a picture of the traceback. Copy, paste and format it. -- anyway, this is solved. – FHTMitchell Jun 21 '18 at 13:58
  • @FHTMitchell, @pLOPeGG; Thanks, but i am using Python 3... – Harry Jun 21 '18 at 13:59
  • @Harry What is the result of `import sys; print(sys.version)` at the top of your script? – FHTMitchell Jun 21 '18 at 13:59
  • @FHTMitchell 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]. Ah, i changed to raw_input, but i still get the error.. – Harry Jun 21 '18 at 14:01
  • @Harry So it's python 2.7.12, not python 3... You still get the exact same error with `raw_input`? – FHTMitchell Jun 21 '18 at 14:02
  • @FHTMitchell, yep...But i noticed that if i enter ''stop, the second ' would be eaten... – Harry Jun 21 '18 at 14:03
  • @Harry Can you please edit your original post with the error you get when using `raw_input`? – FHTMitchell Jun 21 '18 at 14:07
  • @FHTMitchell, i was using a german keyboard. But after i changed to the english one. it was solved...So basically, i have to use the english keyboard, right? – Harry Jun 21 '18 at 14:08
  • err unless you're inputting unicode characters like ß, ä, ö, ü that shouldn't matter. My suggestion is you should upgrade to python 3, use your original code and not care about unicode. – FHTMitchell Jun 21 '18 at 14:12
  • @FHTMitchell, thanks for your suggestions! Thanks! It works. – Harry Jun 21 '18 at 14:13

2 Answers2

0

You are probably using Python2. In Python2 input() returns an int, for having a string you should use raw_input():

while True:
  value = raw_input('enter text: ')
  if value == 'stop':
    print('bye-bye')
    break
  else:
    print('continue!')

Do not use input() in Python2 since it actually evaluates the input, and it can be dangerous. See this other question for the difference from Python3 and Python2 input().

When you change to raw_input(), be sure to save before calling the script

Gsk
  • 2,929
  • 5
  • 22
  • 29
0
def console(input_):
    input_ = input(r'Enter text: ')
    if input_ == str(r'stop'):
        return ("BYE BYE")
    else:
        input_ = input('continue')
    console(input_)

console(input)

Works fine on python3.