0

When I put

input('Enter your name please: ')

In the console, no dialogue box comes up just a >? where I type a word, then this happens. https://imgur.com/lQDIutR

Sorry if this is obvious, I was part-way through learning C++, then switched to this, and it feels so different and alien, I get lost quite easily.

Edit: I am using the IDE PyCharm.

Theroarx
  • 73
  • 2
  • 9

2 Answers2

0

I think you're in python 2.7, that's the reason you're seeing the error.

So here is the detail:

Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input('Enter your name please: ')
Enter your name please: hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'hi' is not defined

However, if you make sure to quote your input while typing, this works.

>>> input('Enter your name please: ')
Enter your name please: "My name is" 
'My name is'
>>> 

This is the same console and now it works fine. This is the expected behaviour of python2. However if you do the same in the python3, it works fine.

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input('Enter your name please: ')
Enter your name please: hi
'hi'
>>> 

You don't need to quote the string input in python3. So you need to quote your string in your input response.

user2906838
  • 1,178
  • 9
  • 20
0

In Python 2.7, you need to use raw_input() instead of input() so that what you input does not get evaluated as Python expression.

>>> input('Enter your name please: ')
Enter your name please: hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'hi' is not defined
>>> raw_input('Enter your name please: ')
Enter your name please: hi
'hi'
>>>
blhsing
  • 91,368
  • 6
  • 71
  • 106