By dtechie

2010-04-10 10:05:06 8 Comments

I am learning python and have this error . I can figure out where\what the error is in the code. File "<string>", line 1, in <module>.

Name = ""
Desc = ""
Gender = ""
Race = ""
# Prompt user for user-defined information
Name = input('What is your Name? ')
Desc = input('Describe yourself: ')

When i run the program

it outputs What is your Name? (i input d )

this gives the error

Traceback (most recent call last):
  File "/python/", line 19, in <module>
    Name = input('What is your Name? ')
  File "<string>", line 1, in <module>
NameError: name 'd' is not defined

This is an example code from Python 3 for Absolute Beginners.


@Daniel G 2010-04-10 10:07:05

In Python 3.0 and above, which the book you are using teaches, input() does what raw_input() did in Python 2, so in that case the code would be correct; however, it appears that you are using an older version of Python (2.6?).

I would recommend going to the Python website and downloading the latest version of Python 3 instead, so you have an easier time following the book.

The immediate problem, given that you are using Python 2, is that you're using input(), which evaluates whatever you give it. What you want to do is get the raw string that the user input:

Name = raw_input("What is your Name? ")

There are lots of little differences between Python 3.x and 2.x, so definitely go get the latest Python 3 if you want to keep using Python 3 for Absolute Beginners.

@kennytm 2010-04-10 10:10:22

You're probably using Python 2.x, where input will eval the user input. Only in Python 3.x input() returns the raw user input.

You can check the version of Python by running python in console, e.g. this is Python 2.6:

~$ python
Python 2.6.5 (r265:79063, Apr  5 2010, 00:18:33) 
[GCC 4.2.1 (Apple Inc. build 5659)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

You can run a specific version of Python (e.g. 3.1) by python3.1:

~$ python3.1
Python 3.1.1 (r311:74480, Jan 25 2010, 15:23:53) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

@dtechie 2010-04-10 10:22:04

Hi kennyTM thanks a lot ! yup it looks like a version conflict :( Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 will upgrade my python version. I would never have figured it out without you guys thanks again :)

@Amber 2010-04-10 10:07:46

In Python 2.x, input() expects something which is a Python expression, which means that if you type d it interprets that as a variable named d. If you typed "d", then it would be fine.

What you probably actually want for 2.x is raw_input(), which returns the entered value as a raw string instead of evaluating it.

Since you're getting this behavior, it looks like you're using a 2.x version of the Python interpreter - instead, I'd go to and download a Python 3.x interpreter so that it will match up with the book you're using.

Related Questions

Sponsored Content

56 Answered Questions

[SOLVED] Calling an external command in Python

27 Answered Questions

15 Answered Questions

[SOLVED] What are metaclasses in Python?

17 Answered Questions

[SOLVED] How can I make a time delay in Python?

28 Answered Questions

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3202475 View
  • 2640 Score
  • 29 Answer
  • Tags:   python list

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

33 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 2647941 View
  • 2315 Score
  • 33 Answer
  • Tags:   python datetime time

17 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

19 Answered Questions

[SOLVED] Python progression path - From apprentice to guru

  • 2010-04-04 00:28:33
  • Morlock
  • 348813 View
  • 659 Score
  • 19 Answer
  • Tags:   python

Sponsored Content