趣学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)
成立