1 # 1.键盘录入的字符串(假设字符串中只包含小写字母和空格)进行加密操作, 2 # 加密的规则是a变d,b变e,c变f,??,x变a,y变b,z变c,空格不变,返回加密后的字符串 3 # 97 98 99 100 x = 120(97) y = 121(98) z = 122(99) 4 5 c_in = input("输入一个字母串:") 6 mi_c = ‘‘ 7 for c in c_in: 8 if 97 <= ord(c) < 120: 9 mi_c += chr(ord(c)+3) 10 elif 120 <= ord(c) <= 122: 11 mi_c += chr(ord(c)-23) 12 else: 13 mi_c += ‘ ‘ 14 print("加密后:",mi_c) 15 16 # 2.键盘录入指定长度生成对应的验证码(由数字和大小写英文字母构成的随机字符串)。 17 #方法一 18 import random 19 yzm = int(input(‘输入验证码位数:‘)) 20 zimu = random.randint(1,yzm) 21 daxie = random.randint(1,zimu) 22 xxie = zimu - daxie 23 shuzi = yzm - zimu 24 str_1 = random.sample(range(0,10),shuzi) #生成数字 25 str_2 = random.sample(range(97,123),xxie) #生成小写字母 26 str_3 = random.sample(range(65,91),daxie) #生成大写字母 27 str_4 = str_2 + str_3 28 str_5 = [] 29 for inn in str_4: 30 str_5.append(chr(inn)) 31 str_6 = str_1 + str_5 32 for inn in str_6: 33 print(inn,end=‘‘) 34 35 #方法二 36 import random 37 yzmlen = int(input(‘输入验证码位数:‘)) 38 sz = list(range(0, 10)) 39 zm = [] 40 zmb = list(range(97, 123)) + list(range(65, 91)) 41 for inn in zmb: 42 zm.append(chr(inn)) 43 mlist = sz + zm 44 yzm = random.sample(mlist, yzmlen) 45 for inn in yzm: 46 print(inn,end=‘‘) 47 48 49 50 # 3.自定义一个列表,最大的与第一个元素交换,最小的与最后一个元素交换,输出列表 51 # 52 lis = [12, 34, 56, 78, 99, 43, 1, 33, 75] 53 max_i = 0 54 min_i = -1 55 for inn in range(1, len(lis)-1): 56 if lis[max_i] < lis[inn]: 57 lis[max_i], lis[inn] = lis[inn], lis[max_i] 58 if lis[min_i] > lis[inn]: 59 lis[min_i], lis[inn] = lis[inn], lis[min_i] 60 print(lis) 61 62 63 #4.自定义一个数字列表,求列表中第二大数的下标 64 65 list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] 66 max_index = 0 67 sec_index = 0 68 for index in range(1,len(list1)-1): 69 if list1[index] > list1[max_index]: 70 max_index = index 71 for index in range(1,len(list1)-1): 72 if max_index != index: 73 if list1[index] > list1[sec_index]: 74 sec_index = index 75 else: 76 continue 77 print("列表",list1,"\n其中第二大的元素%d 索引是 %d"%(list1[sec_index],sec_index)) 78 # # 79 # 5.已知有字符串"I wish you prosperity and a better tomorrow I hope you can do better self-improvement efforts” 80 # a. 获取每个字符出现的次数 81 # b. 获取出现次数最少的字符 82 # c. 获取字符串中的每个字符 存放于列表中 但是列表中不允许有重复的字符, 并对列表进行降序排序 83 # 84 str1 = ‘I wish you prosperity and a better tomorrow I hope you can do better self-improvement efforts‘ 85 ch_dic = {} 86 lis = [] 87 for ch in str1: 88 if ch not in ch_dic: 89 ch_dic[ch] = 1 90 else: 91 ch_dic[ch] += 1 92 if ch not in lis: 93 lis.append(ch) 94 print(ch_dic,lis,sep="\n") 95 96 str_s = None 97 min_ch = min(ch_dic.values()) 98 for k,v in ch_dic.items(): 99 if v == min_ch: 100 str_s = k 101 print("出现最少次数的字符是",str_s) 102 103 lis.sort(reverse=True) 104 print(lis) 105 106 # 6.提取字符串中的数字 进而求和 107 # "i13love14you520" ----> 13 + 14 + 520 = 547 108 109 str = ‘i13love14you520‘ 110 str += ‘end‘ 111 n_sum = 0 112 str1 = ‘‘ 113 lis = [] 114 for i in str: 115 if 48 <= ord(i) <= 57: 116 str1 += i 117 else: 118 if str1 != ‘‘: 119 lis.append(str1) 120 str1 = ‘‘ 121 print(lis) 122 for inn in lis: 123 n_sum += int(inn) 124 print(n_sum )
原文地址:https://www.cnblogs.com/wenmo/p/11272776.html
时间: 2024-10-06 09:24:59