-1

Using python 2.7.12 im using a simple program:

import time
print('Whats Your Name?')
name=input()
print('Happy Birthday to You')
time.sleep(1.5)
print('Happy Birthday to You')
time.sleep(1.8)
print('Happy Birthday Dear '+name)
time.sleep(2)
print('Happy Birthday to You')
time.sleep(1.5)

however when i try to input name i get this:

Whats Your Name? Jeff

Traceback (most recent call last):
  File "E:\Python\happybirthday.py", line 3, in <module>
    name=input()
  File "<string>", line 1, in <module>
NameError: name 'Jeff' is not defined

Why? This seems to happen on every program which requires an input.

Elijah D-R
  • 33
  • 2
  • 11

4 Answers4

1

Change input to raw_input which returns a string.

On Python 2.x input tries to evaluate its input, while raw_input returns a string. On Python 3 input replaces raw_input

jamylak
  • 128,818
  • 30
  • 231
  • 230
1

You need to use raw_input() as opposed to input() as you are using Python 2.x.

In Python 2.x input() will will try and evaluate your input whereas raw_input() will return a string which you can use.

Hope this helps!

0

If you want to pass a string into name from what the user types, you should use raw_input() instead of input().

Daniel
  • 2,345
  • 4
  • 19
  • 36
0

Difference between raw_input() and input():

In Python2.x:

input() is looking for an expression.

if you give 1+1 to input(), it will evaluate it and return 2.
jeff being not an expression, it will throw an error.
if input is "jeff" or 'jeff', input() will return the same string.

raw_input() is looking for a string.

if you give 1+1 to raw_input(), it will return "1+1"(string).
if you give jeff to raw_input(), it will return "jeff".

In Python3.x:

raw_input() is renamed as input() and raw_input() is removed.

Community
  • 1
  • 1
Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41