1

I'm trying to create a really basic program with Python 2.7.10 as displayed below:

Colour = input("What's your favourite colour?")
print (Colour)

After I answer my question, say I answer "Blue", I get this error:

Traceback (most recent call last):
File "C:/Python27/Practise.py", line 1, in <module>
Colour=input("What's your favourite colour?")
File "<string>", line 1, in <module>
NameError: name 'Blue' is not defined

I know this may seem like a noob question, but if I could get your help on how to fix this, that would be great.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
Renma
  • 13
  • 3

1 Answers1

0

In Python2, input tries to take whatever it is given and immediately process it as if it were real Python code. This is called an eval and is a huge security risk in production code (not so much when you're just playing around trying to learn).

From the Python 2 docs on input:

Equivalent to eval(raw_input(prompt)).

To avoid this, replace your input calls with raw_input which will return a string instead.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82