百木园-与人分享,
就是让自己快乐。

Python3.x 基础练习题100例(91-100)

练习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())))

输出结果:

Sat Mar 23 19:37:22 2019
Sat Mar 23 19:37:22 2019
Sat Mar 23 11:37:22 2019

练习92:

题目:
时间函数举例2。

程序:

if __name__ == \'__main__\':
import time

start = time.time()
for i in range(3000):
print(i)
end = time.time()

print(end - start)

输出结果:

0
1
2



2997
2998
2999
0.02006244659423828

练习93:

题目:
时间函数举例3。

程序:

if __name__ == \'__main__\':
import time

start = time.time()
for i in range(10000):
print(i)
end = time.time()
print(\'different is %6.3f\' % (end - start))

输出结果:

0
1
2
3



9996
9997
9998
9999
different is 0.059

练习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:\')
start = time.time()
a = time.time()
guess = int(input(\'input your guess:\'))
while guess != i:
if guess > i:
print(\'please input a little smaller\')

guess = int(input(\'input your guess:\'))
else:
print(\'please input a little bigger\')

guess = int(input(\'input your guess:\'))
end = time.time()
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.\')

练习95:

题目:
字符串日期转换为易读的日期格式。

程序:

from dateutil import parser
dt = parser.parse(\"Aug 28 2015 12:00AM\")
print (dt)

练习96:

题目:
计算字符串中子串出现的次数。

程序:

if __name__ == \'__main__\':
str1 = input(\'请输入一个字符串:\')
str2 = input(\'请输入一个子字符串:\')
ncount = str1.count(str2)
print(ncount)

输出结果:

请输入一个字符串:qwery gdfdaabbbcccabc
请输入一个子字符串:a
3

练习97:

题目:
从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。

程序:

if __name__ == \'__main__\':
filename = input(\'输入文件名:\')
fp = open(filename,\"w\")
ch = input(\'输入字符串:\')
while ch != \'#\':
fp.write(ch)
print(ch)
ch = input(\'\')
fp.close()

练习98:

题目:
从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件\"test\"中保存。

程序:

if __name__ == \'__main__\':
fp = open(\'test.txt\',\'w\')
string = input(\'please input a string:\\n\')
string = string.upper()
fp.write(string)
fp = open(\'test.txt\',\'r\')
print (fp.read())
fp.close()

练习99:

题目:
有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。

程序:

if __name__ == \'__main__\':
import string

fp = open(\'text1.txt\')
a = fp.read()
fp.close()

fp = open(\'text2.txt\')
b = fp.read()
fp.close()

fp = open(\'text3.txt\', \'w\')
l = list(a + b)
l.sort()
s = \'\'
s = s.join(l)
fp.write(s)
fp.close()

练习100:

题目:
列表转换为字典。

程序:

i = [\'a\', \'b\']
l = [1, 2]
print(dict([i, l]))

输出结果:

{\'a\': \'b\', 1: 2}

来源:https://www.cnblogs.com/youcans/p/14500680.html
图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Python3.x 基础练习题100例(91-100)

相关推荐

  • 暂无文章