Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
closed by

Consider the below string test

test = raw_input("enter the test") 

print test

It works fine in Python 2.x

In python 3.x, I do:

test = input("enter the test") 

print test

When I give input string sdas, I get the following error message

Traceback (most recent call last):

 File "/home/ananiev/PycharmProjects/PigLatin/main.py", line 5, in <module>

    test = input("enter the test")

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

NameError: name 'sdas' is not defined

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
In Python 3.x, the input() function behaves differently compared to Python 2.x's raw_input() function. In Python 3.x, the input() function evaluates the user's input as a Python expression, while in Python 2.x, raw_input() simply reads the input as a string.

To fix the error you're encountering in Python 3.x, you need to update your code as follows:

test = input("enter the test")

print(test)

By enclosing the input() function within print(), you ensure that the value entered by the user is displayed correctly.
0 votes
by (26.4k points)

You're executing your Python 3 code with a Python 2 mediator (interpreter). On the off chance that you weren't, your print explanation would hurl a SyntaxError before it at any point incited you for input. 

The outcome is that you're utilizing Python 2's input, which attempts to eval your information (apparently sdas), finds that it's invalid Python, and bites the dust

Want to become an expert in Python? Join the python course fast!

0 votes
by (15.4k points)
In Python 3.x, the input() function behaves differently from Python 2.x's raw_input() function. In Python 3.x, input() interprets the user's input as a Python expression, while raw_input() in Python 2.x treats the input as a plain string.

To resolve the error you encountered in Python 3.x, you need to modify your code as follows:

test = input("Enter the test: ")

print(test)

By using print(test), you ensure that the user's input is displayed correctly.
0 votes
by (19k points)
In Python 3.x, the input() function works differently from Python 2.x's raw_input(). To fix the error, change raw_input() to input() and modify the print statement like this:

test = input("Enter the test: ")

print(test)

This change ensures that the user's input is interpreted correctly and displayed as intended.

Related questions

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...