User123
User123

Reputation: 486

User inputs reserved keyword in Python. There happens an error

I am working on a simple program (just for a joke). A program wants that the user input yes or no (it can be in different languages). But when he enters a reserved word (i. e. keyword) there happens an error because this keyword does some bugs in the code.

My truncated code (maybe it seems unclear because it's truncated):

x = input('Enter yes or no (you can do this in different languages...) ')
x = x.lower()
answersYes = ['yes','si','oui','ioe','inde','tak','ja','da']
answersNo = ['no','ayi','che','leai','nie','ne','nein']
if ' ' in x:
    print('Input just one word!')
else:
    if x in answersYes:
        print('You enteres YES!')
    elif x in answersNo:
        print('You enteres NO!')
    else:
        print('Sorry, but this isn\'t YES nor NO!')

I have done some googling around, but there was no luck yet.

Thank you a lot for any answer!

P.S.

Just one little note:

When I have run the upper script in Python in basic Python IDLE, there wasn't any error, but when I have run this in Spyder, there displayed this message (when I typed 'yes in no' ("in" is a reserved word)):

 File "<ipython-input-49-d1e48c3ddecb>", line 1
    yes in no
        ^
SyntaxError: invalid syntax

Upvotes: 1

Views: 387

Answers (1)

Jake
Jake

Reputation: 2689

I don't get an error. Make sure your spyder is python3-configured or try running with python3 file.py You can also try replacing input() with raw_input()

Upvotes: 1

Related Questions