码迷,mamicode.com
首页 > 编程语言 > 详细

简明Python教程笔记(二)----用户交互raw_input()

时间:2014-06-24 16:57:50      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:python   raw_input()   用户交互   

raw_input()

    python内建函数

    将所有输入看做字符串,返回字符串类型

 input()对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )

 input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数

例子:

#!/usr/bin/env python
this_year = 2014
name = raw_input(‘please input your name:‘)
age1 = raw_input("how old are you?")
age = int(raw_input(‘how old are you?‘)) #将字符串型转为int型

print "hello",name,‘\n‘
print "you are",age1,‘years old!‘
print "you are",age,‘years old!‘
print "so you were born in ", this_year - age

print "so you were born in ", this_year - age1

脚本执行结果:

C:\Users\d\Desktop>python jiaohu.py
please input your name:cuijuntao
how old are you?25
how old are you?26
hello cuijuntao

you are 25 years old!
you are 26 years old!
so you were born in  1988
so you were born in
Traceback (most recent call last):
  File "jiaohu.py", line 11, in <module>
    print "so you were born in ", this_year - age1
TypeError: unsupported operand type(s) for -: ‘int‘ and ‘str‘

因为age1不是int型,无法做算法运算而报错。

本文出自 “1058223494” 博客,请务必保留此出处http://4708705.blog.51cto.com/4698705/1430055

简明Python教程笔记(二)----用户交互raw_input(),布布扣,bubuko.com

简明Python教程笔记(二)----用户交互raw_input()

标签:python   raw_input()   用户交互   

原文地址:http://4708705.blog.51cto.com/4698705/1430055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!