1、输出 9*9 乘法口诀表(注意格式)
def table():
#在这里写下您的乘法口诀表代码吧!
for i in range(1,10):
k=i+1
for j in range(1,k):
print('%d*%d=%2d'%(i,j,i*j),end='\t')#end取消自动换行
print('\n')
if __name__ == '__main__':
table()
部分结果:
1*1= 1
2*1= 2 2*2= 4
3*1= 3 3*2= 6 3*3= 9
4*1= 4 4*2= 8 4*3=12 4*4=16
作业二:查找特定名称文件
遍历”Day1-homework”目录下文件;
找到文件名包含“2020”的文件;
将文件名保存到数组result中;
按照序号、文件名分行打印输出
#导入OS模块
import os
#待搜索的目录路径
path = "Day1-homework"
#待搜索的名称
filename = "2020"
#定义保存结果的数组
result = []
def find_file(path,name):
for _file in os.listdir(path):
this_path=os.path.join(path,_file)
if name in this_path:
result.append(this_path)
continue
if os.path.isdir(this_path):
find_file(this_path,name)
return result
def findfiles():
#在这里写下您的查找文件代码吧!
outs=find_file(path,filename)
i=1
for out in outs:
print('第%d个文件名为:'%i,out)
i+=1
if __name__ == '__main__':
findfiles()
结果:
第1个文件名为: Day1-homework/18/182020.doc
第2个文件名为: Day1-homework/4/22/04:22:2020.txt
第3个文件名为: Day1-homework/26/26/new2020.txt