Python的一些例题

1.用递归遍历文件夹,如果是文件,打印出路径,如果是目录,继续循环打开

import os
def allfiles(path):
for i in os.listdir(path):
filepath=os.path.join(path,i)
if os.path.isfile(filepath):
print(filepath)
else:
allfiles(filepath)
filepath=os.getcwd()
allfiles("文件")
2,用装饰器计算函数运行时间

import time

def A(a):
   def B():
start =time.time()
a()
end = time.time()
print(end - start)
return B
@A
def func1():
for i in range(1000000):
type("hello")
func1()
@A
def func2():
for i in range(1000000):
isinstance('hello',str)
func2()

3.正则验证IP地址
import re
a = '255.255.255.255'
b = '0.10.99.255'
c = '192.188.0.1'
d = '200.249.254.110'
e = '256.256.256.256'
try:
resa = re.search('((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}',a).group()
print(resa)
resb = re.search('((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', b).group()
print(resb)
resc = re.search('((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', c).group()
print(resc)
resd = re.search('((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', d).group()
print(resd)
rese = re.search('((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', e).group()
print(rese)
except:
print('地址不对')
4.注册和登陆程序(封装成类)
class login:
def __init__(self,id,mima):
self.id = id
self.mima = mima
def judgment2(self): #登陆程序,经过judgment1(self):以后,要么在judgment1(self):已经退出,要么就一定有账号,循环登录
f2 = open('users', 'r', encoding='utf-8')
x2 = eval(f2.read())
while True:
name2 = input('用户名: ')
password2 = input('密码: ')
if (name2, password2) in x2.items():
f2.close()
break
else:
print('用户名或密码错误请重新输入')
choice = int(input('重新登陆请输入1,退出请输入2: '))
if choice == 1:
continue
elif choice == 2:
f2.close()
return ('已退出')
return ('用户{}登陆成功'.format(name2))
def judgment1(self): #判断输入的用户名和密码是否在user文件中,在的话登陆成功,不在的话注册后跳转到judgment2(self):,重新登陆也跳转到judgment2(self):
f1 = open('users','r+',encoding='utf-8')
x1= eval(f1.read())
name = self.id
password = self.mima
if (name,password) not in x1.items():
print('用户名或密码错误')
choice = int(input('注册请输入1,重新登陆请输入2,退出请输入3: '))
if choice == 1:
name1 = input('用户名: ')
password1 = input('密码: ')
repassword1 = input('再次输入密码: ')
if name1 in x1.keys():
print('用户名重复,请重新输入')
else:
if not password1 or not name1 or not name1 and not password1:
print('用户名或密码不能为零')
elif password1 != repassword1:
print('两次输入密码不一致')
elif password1 == repassword1:
new_dic = {name1:password1}
x1.update(new_dic)
f1.seek(0)
f1.truncate()
f1.seek(0)
f1.write(str(x1))
print('用户{}注册成功'.format(name1))
f1.close()
return self.judgment2()
elif choice == 2:
return self.judgment2()
elif choice == 3:
return ('已退出')
else:
return ('用户{}登陆成功'.format(name))
denglu = login(input('用户名: '),input('密码: '))
print(denglu.judgment1())
5.用生成器实现斐波那契数列
def func(num):
a = 0
b = 1
for i in range(num):
c = a
a = b
b = c + b
yield c
res = func(10)
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
以上均为我自己写出来的,可能有问题,欢迎大家指点
posted on 2022-02-23 20:32  请你一定要记得我好吗  阅读(55)  评论(0)    收藏  举报