0

I am using Enthought Canopy compiler. I am trying to get a word input from user using this code below:

starting_day = str(input("Enter the day you will be leaving: "))

however whenever I test the var by running the program I keep getting the error below after assigning a string of letters to the variable(it's runs when I give it numbers)

Enter the day you will be leaving: monday

NameError                                 Traceback (most recent call last)

C:\Users\user\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.1.0.1371.win-x86\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    174             else:

    175                 filename = fname

--> 176             exec compile(scripttext, filename, 'exec') in glob, loc

    177     else:

    178         def execfile(fname, *where):

c:\users\user\appdata\local\temp\tmpnwkfhu.py in <module>()

----> 1 starting_day = str(input("Enter the day you will be leaving: "))

      2 

      3 lenght_of_stay = int(input("Enter the number of days you will say for: "))

      4 

      5 print(starting_day, lenght_of_stay)

<string> in emulated_input(prompt)

<string> in <module>()

NameError: name 'monday' is not defined
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • how do I change it to 3 – user133745 May 05 '14 at 14:01
  • You might find [this question](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) useful in terms of taking user input. – jonrsharpe May 05 '14 at 14:06
  • @user133745: don't ask questions in the comment, [edit] your question instead or [ask a new one](http://stackoverflow.com/questions/ask) – jfs May 05 '14 at 14:09
  • @J.F.Sebastian: Not that question; asking for recommendations is off-topic, and changing your question to a new one is also frowned upon. Editing the question is reserved for clarifying the current issue, not add new ones. – Martijn Pieters May 05 '14 at 14:17
  • @MartijnPieters: *"how do I change it to 3"* shouldn't be in the comments to OPs question. period. – jfs May 05 '14 at 14:18
  • @J.F.Sebastian: (misunderstood, trying again): ah, that comment was in response to my short version of the answer below, which I deleted in favour of an actual answer. But the OP misinterpreted it as 'update to ask for a Python 3 bundle recommendation too'. – Martijn Pieters May 05 '14 at 14:21

1 Answers1

1

You are running this with Python 2, not Python 3.

In Python 2, input() passes all input to eval(); the string monday is interpreted as a Python name, which throws a NameError because it is not defined.

You can see this in your traceback as well; exec in Python 3 is a function, in Python 2 it is a statement. The traceback uses it as a statement (no parenthesis). Most of all, you are using Enthought Canopy, which can only support Python 2.

Either use raw_input() instead, or run your code with an actual Python 3 interpreter.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • how do i change to py 3 – user133745 May 05 '14 at 14:02
  • @user133745: You can't with Canopy. Install a different Python 3 distribution. – Martijn Pieters May 05 '14 at 14:02
  • why is there `def execfile()` in `py3compat.pyc` file in the traceback if it doesn't support Python 3? – jfs May 05 '14 at 14:06
  • @J.F.Sebastian: that's [IPython compatibility layer](https://github.com/ipython/ipython/blob/master/IPython/utils/py3compat.py); it helps keep the codebase compatible across Python 2 and 3. – Martijn Pieters May 05 '14 at 14:09
  • Do any of you know a good free Python 3 Distribution that is good for people new to programming – user133745 May 05 '14 at 14:11
  • yes. I've only noticed IPython in the path now i.e., whether or not Canopy is Python 2 only; IPython supports both Python 2 and 3. Sorry for the noise. – jfs May 05 '14 at 14:12
  • @J.F.Sebastian: The `NameError` is a dead ringer for a Python 2 `input()` call. The rest of the evidence I presented is just icing. :-) – Martijn Pieters May 05 '14 at 14:13
  • 1
    @user133745: You could try [Anaconda](https://store.continuum.io/cshop/anaconda/); it also comes with iPython but supports Python 3 as well as Python 2. – Martijn Pieters May 05 '14 at 14:15
  • @MartijnPieters: there is no doubt that OP ran Python 2 interpreter. I meant that it doesn't prove that the codebase itself is incompatible with Python 3 (even if it is) – jfs May 05 '14 at 14:19