Issue with accepting strings from user using python's input function?

it accepts numbers without error, however when i input text it says the “text” is not defined.

Hi @vasank1958,

In order to avoid problems like this, please:

  • put the following on the first line of your python file, to indicate it’s a python executable:
#! /usr/bin/env python
  • Enclose your string inputs within quotes: "sfa" instead of just sfa.

Please let us know how it goes.


By the way, welcome to the community!

Hi @vasank1958,

that happens because you executed the script using python2.

In order to make the script work, you have to run the code with python3:

python3 fav_movie.py

This already solves the problem, but if you need an explanation, we have it below


The explanation is:

In python2, input works for numbers, but for strings you have to use raw_input.
In python3, however, input can also be used for strings.

So, to solve the problem you could either:

  1. Run your script with python3:

python3 fav_movie.py

  1. Run with python2, but change input by raw_input. In this case, the code would be:

movie = raw_input ('What is your favorite movie? ')

and you could execute it normally with python2, but giving that the course is about python3, I’d suggest you the first option.

3 Likes