python中的input函数和raw_input函数的区别

来源:互联网 发布:pk10网站源码 编辑:程序博客网 时间:2024/05/22 14:56

input()和raw_input()函数的区别:

   首先,说明eval函数:eval函数的参数必须是字符串形式。eval函数是将字符串转换为算式运算。

       如:1. 参数为空,出错。

            2. 说明参数必须为字符串string对象。

            3.  eval函数将字符串转换为算式表达式求值,会在历史记录(局部空间,全局空间)寻找变量名的值。如x的值。

  其次,input函数。

          input函数是输入函数,如果输入的变量为字符串或字符,且该变量名在历史记录中有过赋值,则正确。 否则出错。   输入整数,正确。


  最后,raw_input函数,是将输入的变量以字符串存储起来。输入值都正确。不会出错。raw_input函数比input函数更通用。

eval(expression[,globals[, locals]])

The arguments are a Unicode or Latin-1 encoded string and optional globals and locals. If provided,globals must be a dictionary. If provided, locals can be any mapping object.

input([prompt])

Equivalent to eval(raw_input(prompt)).

If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.

raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read,EOFError is raised. Example:

>>> s = raw_input('--> ')--> Monty Python's Flying Circus>>> s"Monty Python's Flying Circus"

If the readline module was loaded, thenraw_input() will use it to provide elaborate line editing and history features.


0 0
原创粉丝点击