Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in python d not defined. [duplicate]

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/chargen.py", 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.

like image 254
dtechie Avatar asked Apr 10 '10 10:04

dtechie


People also ask

Why does Python keep saying my variable is not defined?

The Python "NameError: name is not defined" occurs for multiple reasons: Accessing a variable that doesn't exist. Accessing a variable, function or class before it is declared. Misspelling the name of a variable, a function or a class (names are case-sensitive).

How do I fix the NameError in Python?

To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.

Is not defined in Python?

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

Is not defined input python?

The Python "NameError: name 'raw_input' is not defined" occurs when we use the raw_input() function in Python 3. To solve the error, use the input() function instead of raw_input in Python 3 applications, e.g. s = input('Your name: ') . Here is an example of how the error occurs.


1 Answers

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 www.python.org and download a Python 3.x interpreter so that it will match up with the book you're using.

like image 159
Amber Avatar answered Oct 02 '22 09:10

Amber