import random
result=[]
for i in range(3):
temp=[]
while True:
if len(temp)==3:
break
num=random.randint(1,1000)
if num%2!=0 and (num%3==0 and num%5==0):
temp.append(num)
else:
continue
result.append(temp)
print(result)
209、使用26个小(大)写字母和26个小(大)写字母对应的ascii生成一个字典
lower_letters_dict={}
for i in range(97,123):
lower_letters_dict[chr(i)]=i
print(lower_letters_dict)
upper_letters_dict={}
for i in range(65,91):
upper_letters_dict[chr(i)]=i
print(upper_letters_dict)
import random
def letters_password(s):
if not isinstance(s,str):
return s
if not s.isalpha():
return s
letter_ascii_list=list(range(97,123)) #ascii码列表
#print(letter_ascii_list)
letter_password_dict={} #字母与ascii码对应关系的字典
for i in range(97,123):
password_digit=random.choice(letter_ascii_list) #随机从ascii码列表中选择一个数字
letter_password_dict[chr(i)]=password_digit #将该数字与字母写入对应关系字典
letter_ascii_list.remove(password_digit) #从ascii码列表中删除该数字
#print("字母加密对应关系:",letter_password_dict)
s_psw=""
for i in s:
s_psw+=(str(letter_password_dict[i])+" ")
return s_psw
print(letters_password("123sdd"))
print(letters_password("cax"))
211、将上题的加密串进行解密
import random
def not_letters_password(s):
if not isinstance(s,str):
return s
letter_ascii_list=list(range(97,123)) #ascii码列表
print(letter_ascii_list)
letter_password_dict={} #字母与ascii码对应关系的字典
for i in range(97,123):
password_digit=random.choice(letter_ascii_list) #随机从ascii码列表中选择一个数字
letter_password_dict[str(password_digit)]=chr(i) #将该数字与字母写入对应关系字典
letter_ascii_list.remove(password_digit) #从ascii码列表中删除该数字
print("字母加密对应关系:",letter_password_dict)
s_list=s.split(" ")
print(s_list)
result=[]
for i in s_list:
letter=letter_password_dict[i]
result.append(letter)
return "".join(result)
print(not_letters_password("111 118 114"))
def list_to_dict(l):
if not isinstance(l,list):
return l
result={}
for i in l:
if isinstance(i,(int,float,complex,str,tuple)):
result[i]=1
return result
print(list_to_dict([1,2.0,'abcds',[1,2,3],(1,2),{'a':1,'b':2},set([1,2,3])]))
214、return 语句分析
def f(a,b):
try:
raise IOError
return a
except IOError:
a = a + b
print(a)
result = f(1,2)
print(result)
#上述方法执行的打印信息是什么?
#3
#None
#分析:try语句遇到 raise IOError语句不会执行return a 语句,执行except里面的语句,所以会打印3,
#因为函数没有获取到返回值,所以会打印None
217、给定字符串s=“i Am a gOod boy baby!!” 判断一个字符串中字母是否全部为小写
s="i m a god boy baby!!"
for i in s:
if i>='A' and i<='Z':
print("不全是小写字母")
break
else:
print("全部是小写字母")