Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

This is my code it works fine on Python 3 but doesn't work on Python2

This is the code:

while True:

    userCommand = input("> ").lower()

    if userCommand == "help" or userCommand == "?":

        print("not yet...")

    else:

        print("Command not recognised.")

When I type"help", I am getting this error:

Traceback (most recent call last):

File "customCLI.py", line 2, in <module>

userCommand = input("> ").lower()

AttributeError: '_Helper' object has no attribute 'lower'

The other error I am getting is :

Traceback (most recent call last):

File "customCLI.py", line 2, in <module>

userCommand = input("> ").lower()

File "<string>", line 1, in <module>

NameError: name 'blahblah' is not defined

Any help will be appreciated 

1 Answer

0 votes
by (36.8k points)
edited by

In Python 2 and 3 because of version difference inputting statements slightly change to know click here.

For your code, you can use the raw_input in when you are working on python2

try:

  input = raw_input

except NameError:

  pass

If you are a beginner and want to know more about Data Science the do check out the Data Science course 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...