使用input()时出现NameError [英] NameError when using input()

查看:150
本文介绍了使用input()时出现NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那我在做什么错了?

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

但是,我只能得到

  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

推荐答案

您在python 2中使用的是input而不是raw_input,它会将输入评估为python代码.

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()等同于eval(raw_input())

  • input
  • raw_input

此外,您正在尝试将烧杯"转换为整数,这没有多大意义.

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

 

您可以用raw_input这样替换您脑海中的输入:

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

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

并使用input:

answer = Beaker        # raises NameError, there's no variable named Beaker
if answer == "Beaker":
   print("Correct!")

这篇关于使用input()时出现NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆