题目91:时间函数举例1。
if __name__ == '__main__':
import time
print(time.ctime(time.time()))
print(time.asctime(time.localtime(time.time())))
print(time.asctime(time.gmtime(time.time())))
Mon Apr 23 03:07:05 2018
Mon Apr 23 03:07:05 2018
Sun Apr 22 19:07:05 2018
Press any key to continue . . .
题目92:时间函数举例2
if __name__ == '__main__':
import time
start = time.time()
for i in range(30):
print(i)
end = time.time()
print(end - start)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
0.3599512577056885
Press any key to continue . . .
题目93:时间函数举例3。
if __name__ == '__main__':
import time
start = time.clock()
for i in range(30):
print(i)
end = time.clock()
print('different is %6.3f' % (end - start))
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
different is 0.144
Press any key to continue . . .
题目94:时间函数举例4,一个猜数游戏,判断一个人反应快慢。
if __name__ == '__main__':
import time
import random
play_it = input('do you want to play it.(\'y\' or \'n\')')
while play_it == 'y':
c = input('input a character:\n')
i = random.randint(0,2**32) % 100
print('please input number you guess:\n')
start = time.clock()
a = time.time()
guess = int(input('input your guess:\n'))
while guess != i:
if guess > i:
print('please input a little smaller')
guess = int(input('input your guess:\n'))
else:
print('please input a little bigger')
guess = int(input('input your guess:\n'))
end = time.clock()
b = time.time()
var = (end - start) / 18.2
print(var)
# print 'It took you %6.3 seconds' % time.difftime(b,a))
if var < 15:
print('you are very clever!')
elif var < 25:
print('you are normal!')
else:
print('you are stupid!')
print('Congradulations')
print('The number you guess is %d' % i)
play_it = input('do you want to play it.')
do you want to play it.('y' or 'n')y
input a character:
34
please input number you guess:
input your guess:
18
please input a little bigger
input your guess:
21
please input a little bigger
input your guess:
88
1.6283611771936324
you are very clever!
Congradulations
The number you guess is 88
do you want to play it.
题目95:数数有多多少个字符串
if __name__ == '__main__':
str1 = input('请输入一个字符串:\n')
str2 = input('请输入一个子字符串:\n')
ncount = str1.count(str2)
print( ncount)
请输入一个字符串:
python
请输入一个子字符串:
n
1
Press any key to continue . . .
题目96:从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。
if __name__ == '__main__':
from sys import stdout
filename = input('输入文件名:\n')
fp = open(filename,"w")
ch = input('输入字符串:\n')
while ch != '#':
fp.write(ch)
stdout.write(ch)
ch = input('')
fp.close()
runoobfile.txt
输入字符串:
runoob
runoob
google
google#
题目97:列表转换为字典。
i = ['a', 'b']
l = [1, 2]
print( dict([i,l]))
{'a': 'b', 1: 2}
Press any key to continue . . .
题目98:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。
if __name__ == '__main__':
import string
fp = open('test1.txt')
a = fp.read()
fp.close()
fp = open('test2.txt')
b = fp.read()
fp.close()
fp = open('test3.txt','w')
l = list(a + b)
l.sort()
s = ''
s = s.join(l)
fp.write(s)
fp.close()