趣学python第7章使用模块

趣学python第7章使用模块

1.time模块

import time
#引入时间模块
print time.time()

=================== RESTART: C:\Python27\lianxi\2018123.py ===================
1517052225.95

打印出一串数字

更改打印函数,

print (time.asctime())

Sat Jan 27 19:25:15 2018

只有打印asctime(),才显示正确的时间。这个函数的作用是作为一个字符串返回当前的日期和时间。

2.sys模块,用来输入一个值

sys中有一个特别的对象stdin标准输入,其中一个函数叫readline,可以用来读取来自键盘的一行文本输入,直到按下回车键为止。

import sys
print(sys.stdin.readline())
这段代码输入数字没问题,到了字符串就报错

print(sys.stdin.readline())
>>> 456
456

print(sys.stdin.readline())
>>> s


Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    s
NameError: name 's' is not defined
>>> 
现在加入if来判断

import sys
print('please input your age')
age =int( sys.stdin.readline())
if age >= 12 and age <= 19:
	print (age )
else :
	print('it is not possible as a student'
这个年龄段是个中学生,

如果把这个判断放入到一个函数中

def age_define(age):
	if age >= 12 and age <= 19:
		print (age )
	else :
		print('it is not possible as a student')	
age_define(9)

成立
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值