Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1781282
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-04-21 14:15:06

先看一段操作:
[13:51 t /tmp]$ python
Python 2.7.11 (default, Mar 31 2016, 20:46:51)
[GCC 5.3.1 20151207 (Red Hat 5.3.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> name=input("Your name:")
Your name:talen    #使用input()输入内容不加引号
Traceback (most recent call last):
  File "", line 1, in
  File "", line 1, in
NameError: name 'talen' is not defined
>>> name=input("Your name:")
Your name:"talen"    #使用input()输入内容添加引号
>>> print name
talen
>>> name=raw_input("Your name:")
Your name:talen        #使用raw_input()输入内容不加引号
>>> print name
talen
>>>
从上面的例子可以看出,如果想输入字符串,input()的输入内容必须使用''号,否则会被认为是变量名。
>>> num=input("Your num:")
Your num:897
>>> type(num)

>>> num2=raw_input("Your num:")
Your num:897
>>> type(num2)

>>>
从这个操作来看,input()接受了输入的数据类型,raw_input()把输入当作字符串来看。所以input()输入必须进行数据类型的转换才能正确表达第一个例子中的输入。
input([prompt])

Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a will be raised. Other exceptions may be raised if there is an error during evaluation.

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

Consider using the function for general input from users.



阅读(1521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~