python d 中的错误未定义. [英] error in python d not defined.

查看:39
本文介绍了python d 中的错误未定义.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 python 并且有这个错误.我可以弄清楚代码中的错误在哪里\是什么.文件",第 1 行,.

I am learning python and have this error . I can figure out where\what the error is in the code. File "<string>", line 1, in <module>.

Name = ""
Desc = ""
Gender = ""
Race = ""
# Prompt user for user-defined information
Name = input('What is your Name? ')
Desc = input('Describe yourself: ')

当我运行程序时

它输出你叫什么名字?(我输入 d )

it outputs What is your Name? (i input d )

这给出了错误

Traceback (most recent call last):
  File "/python/chargen.py", line 19, in <module>
    Name = input('What is your Name? ')
  File "<string>", line 1, in <module>
NameError: name 'd' is not defined

这是 Python 3 中面向绝对初学者的示例代码.

This is an example code from Python 3 for Absolute Beginners.

推荐答案

在 Python 2.x 中,input() 需要 Python 表达式,这意味着如果您键入 d 它将它解释为一个名为 d 的变量.如果你输入"d",那就没问题了.

In Python 2.x, input() expects something which is a Python expression, which means that if you type d it interprets that as a variable named d. If you typed "d", then it would be fine.

对于 2.x,您可能真正想要的是 raw_input(),它将输入的值作为原始字符串返回,而不是对其求值.

What you probably actually want for 2.x is raw_input(), which returns the entered value as a raw string instead of evaluating it.

既然你得到了这种行为,看起来你使用的是 2.x 版本的 Python 解释器 - 相反,我会去 www.python.org 并下载 Python 3.x 解释器,以便它与您正在使用的书相匹配.

Since you're getting this behavior, it looks like you're using a 2.x version of the Python interpreter - instead, I'd go to www.python.org and download a Python 3.x interpreter so that it will match up with the book you're using.

这篇关于python d 中的错误未定义.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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