Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError when using input() [duplicate]

So what am I doing wrong here?

answer = int(input("What is the name of Dr. Bunsen Honeydew's assistant?"))
if answer == ("Beaker"):
    print("Correct!")
else:
    print("Incorrect! It is Beaker.")

However, I only get

  Traceback (most recent call last):
  File "C:\Users\your pc\Desktop\JQuery\yay.py", line 2, in <module>
    answer = int(input("What is the name of Dr. Bunsen Honeydew's assistant?"))
  File "<string>", line 1, in <module>
      NameError: name 'Beaker' is not defined
like image 833
user2329649 Avatar asked Jul 17 '26 16:07

user2329649


1 Answers

You are using input instead of raw_input with python 2, which evaluates the input as python code.

answer = raw_input("What is the name of Dr. Bunsen Honeydew's assistant?")
if answer == "Beaker":
   print("Correct!")

input() is equivalent to eval(raw_input())

  • input
  • raw_input

Also you are trying to convert "Beaker" to an integer, which doesn't make much sense.

 

You can substitute the input in your head like so, with raw_input:

answer = "Beaker"
if answer == "Beaker":
   print("Correct!")

And with input:

answer = Beaker        # raises NameError, there's no variable named Beaker
if answer == "Beaker":
   print("Correct!")
like image 113
Pavel Anossov Avatar answered Jul 20 '26 04:07

Pavel Anossov