热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

pythoninputnameerror_使用input()时出现NameError

SowhatamIdoingwronghere?answerint(input(WhatisthenameofDr.BunsenHoneydewsassistant?))ifa

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

answer = int(input("What is the name of Dr. Bunsen Honeydew's assistant?"))

File "", line 1, in

NameError: name 'Beaker' is not defined

解决方案

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())

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!")



推荐阅读
author-avatar
萝莉吴文娟
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有