1

The code is pretty simple im just starting programming in python

the code

man = input ("what's your name mister")
print("his name is "+man)

the message i get after running the program

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    alex
NameError: name 'alex' is not defined
Klaus D.
  • 13,874
  • 5
  • 41
  • 48
kh med
  • 39
  • 11
  • Are you sure that's all the code? The traceback implies that you are referencing a variable named `alex`, but your code does not show that. – mipadi Jul 30 '15 at 18:02
  • 1
    Is this python 2 or 3? – DJMcMayhem Jul 30 '15 at 18:02
  • possible duplicate of ["NameError: name '' is not defined" after user input in Python](http://stackoverflow.com/questions/2090706/nameerror-name-is-not-defined-after-user-input-in-python) – TigerhawkT3 Jul 30 '15 at 18:03
  • it's python 3.4.3 and the name alex i wrote it after running the program . – kh med Jul 30 '15 at 18:04
  • 1
    Are you 100% sure it's Python 3.4.3? What does it say if you do `import sys; print(sys.version)`? – Kevin Jul 30 '15 at 18:09
  • it take me to another window with this : Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> (it's the same window i get when i run my program) – kh med Jul 30 '15 at 18:13
  • Possible duplicate of [input() error - NameError: name '...' is not defined](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined) – pppery Oct 25 '19 at 00:17

2 Answers2

1

In python 2, "input" takes what you enter, and tries to treat it like a python expression. So it treats your string "alex" as the name of a variable. If you do not have a variable named "alex", it creates an error trying to look for it. Here's an example

alex = "Hello world"
x = input()
print x

If I enter "alex" in the input, this will print "Hello world." If you want to take a string as input instead, use raw_input().

alex = "Hello world"
x = raw_input()
print x

This would print "alex", not "hello world."

If you are using python 3, "input" behaves the exact same way as "raw_input" does in python 2. I just ran your code in python 3 and did not get any error, so you are probably using 2.

Also, here is more info about input and raw_input.

Community
  • 1
  • 1
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
1

Thank you guys for trying to find out the error i found out the mistake , i was clicking Run And then Python shell instead i clicked in run module and it works , im sorry again

kh med
  • 39
  • 11