输入数据未定义 [英] Input data not defined

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

问题描述

我正在编写一些代码,需要用户输入要在程序中使用的文件:

I am writing some code and need the user to input a file for use in the program:

file=input('input file name')

但是,每当您输入文件名(或与此相关的任何内容)时,都会弹出错误消息,提示未定义任何刚刚输入的内容,并结束了程序.是什么导致这种情况发生?

however, whenever you then input a file name (or anything for that matter) an error pops up saything that whatever has just been input is not defined and ends the program. What is causing this to happen?

谢谢

推荐答案

这很重要:

input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).

输入将尝试eval您的输入

检查

In [38]: l =  input("enter filename: ")
enter filename: dummy_file
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
C:\Python27\<ipython-input-37-b74d50e2a058> in <module>()
----> 1 l =  input("enter filename: ")

C:\Python27\<string> in <module>()

NameError: name 'dummy_file' is not defined


In [39]: input /?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:<built-in function input>
Namespace:  Python builtin
Docstring:
input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).

In [40]: file = raw_input("filename: ")
filename: dummy_file

In [41]: file
Out[41]: 'dummy_file'

使用raw_input有它的缺点.

这篇关于输入数据未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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