0429---每日习题 菲薄纳西数列 正则ip匹配

时间:2022-01-08 18:55:01

#8、打印斐波拉契数列前n项

def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2) for i in range(1,9):
print(fib(i))

C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\4\20190428.py
1
1
2
3
5
8
13
21

#9、检查ipV4的有效性,有效则返回True,否则返回False,(提示使用split函数进行分割)

def check_ipv4_addr(ipAddr):
addr =ipAddr.split(".")#将ip切割成列表
if len(addr) != 4:#判断ip列表长度
return False
else:
tag =True
for i in range(len(addr)):
try:
addr[i]=int(addr[i])#ip必须是数字
if addr[i]<=255 and addr[i]>=0:#判断ip范围
pass
else:
tag =False
except:
tag =False
return tag print(check_ipv4_addr("192.168.133.4"))
print(check_ipv4_addr("101.0.256.112"))

#正则匹配

import re

def check_ipv4_addr(ipAddr):
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(ipAddr):
return True
else:
return False print(check_ipv4_addr("191.168.35.257"))
print(check_ipv4_addr("11.0.0.112"))

#10、检测密码强度
'''
c1 : 长度>=8
c2: 包含数字和字母
c3: 其他可见的特殊字符
强:满足c1,c2,c3
中: 只满足任一2个条件
弱:只满足任一1个或0个条件
'''

password=input("请输入密码:")
def check_len(password):
if len(password)>=8:
return True
else:
return False def check_num_letter(password):
for i in password:
if password.isdigit() and password.isalpha():
return True
else:
return False def check_punctuation(password):
import string
result =False
for i in string.punctuation:
if i in password:
result =True
return result def check_strength(password):
count =0
if check_len(password):
count +=1
if check_num_letter(password):
count +=1
if check_punctuation(password):
count +=1 if count ==3:
print("密码强")
elif count ==2:
print("密码中")
else:
print("密码弱")
check_strength(password)