1 # ===================================================================== 2 # 自定义异常 3 # class FUSHUEXCEPTION(Exception): 4 # def __init__(self,msg): 5 # self.message = msg 6 # try: 7 # a = int(input("被除数")) # ValueError 8 # b = int(input("除数")) 9 # if b < 0: 10 # raise FUSHUEXCEPTION("不能为负") 11 # c = a/b # ZeroDivisionError 12 # print("%d/%d=%.2f"%(a,b,c)) 13 # except FUSHUEXCEPTION as e: 14 # print(e.message) 15 # ===================================================================== 16 # PYTHON 异常处理 17 # try: 18 # a = int(input("被除数")) # ValueError 19 # b = int(input("除数")) 20 # # + - * / // ** 21 # c = a/b # ZeroDivisionError 22 # print("%d/%d=%.2f"%(a,b,c)) 23 # except ZeroDivisionError: 24 # print("除数不能为零") 25 # except ValueError: 26 # print("输入必须是数字!") 27 # # except (ZeroDivisionError,ValueError): # 不能确定具体哪里错 28 # # print("错误") 29 # else: 30 # print("else0# 无异常,才执行") 31 # finally: 32 # print("finally0# 无论是否异常,都执行") 33 # alist = [0,1,2] 34 # print(alist) 35 # ===================================================================== 36 # ===================================================================== 37 # ''' 38 # 6、按照控制台提示输入1~3之间任一个数字,程序将输出相应的课程名称根据键盘输入进行判断。v 39 # (1)输入1:输出"Python语言设计"v(2)输入2:输出"Java语言程序设计"↓(3)输入3:输出“数据结构”v 40 # (4)如果输入其他数字,则输出该课程不存在 41 # (5)如果输入非数字字符,则抛出异常提示“输入错误”,(6)不管输入是否正确,均输出“欢迎提出建议!” 42 # 43 # ''' 44 # try: 45 # a = int(input("1-3\n")) 46 # if a == 1: 47 # print("Python语言设计") 48 # elif a == 2: 49 # print("Java语言程序设计") 50 # elif a == 3: 51 # print("数据结构") 52 # else: 53 # print("该课程不存在") 54 # except ValueError: 55 # print("输入错误") 56 # finally: 57 # print("欢迎提出建议!") 58 # ===================================================================== 59 # ''' 60 # 2、python中在定义函数时,若无法得知参数的个数,则可以定义可变长度的参数列表。v 61 # 按照要求编写函数: 62 # (1)定义一个函数mvavg,该方法使用可变长参数,计算任意众指定数字的平均值v 63 # (2)实现一个带默认参数的函数ceshi函数,可以计算m 的n次方,n设置默认值为5。v 64 # (3)测试上述函数 65 # ''' 66 # def myavg(*a): 67 # sum = 0 68 # for x in a: 69 # sum += x 70 # return (sum/len(a)) 71 # def ceshi(m,n=5): 72 # return m**n 73 # if __name__ == "__main__": 74 # print(myavg(1,2,3,4)) 75 # print(ceshi(2)) 76 # print(ceshi(2,3)) 77 # else: 78 # print(__name__) 79 # # ===================================================================== 80 # 递归 81 # def fact(n): 82 # if n==1: 83 # return 1 84 # return n*fact(n-1) 85 # print(fact(5)) 86 # ===================================================================== 87 # ''' 88 # 4、按要求编写程序 89 # (1)封装自己的数学模块MyMath.py,要求如下:v①有一个PI变量,值为3.14159w 90 # ②有一个函数mvmax,完成求一组数据的最大值的功能 91 # (2)编写测试模块test.py,输出PI的值,调用函数mymax求一组数的最大值并输出。 92 # 运行效果如下: v 93 # PI的值为:3.14 94 # 7,22,3,9,12的最大值为:22 95 # 9'77,33的最大值为:77 96 # ''' 97 # # test.py 98 # PI = 3.14159 99 # def mymax(*args): 100 # return max(args) 101 102 # import myMath # 封装自己的数学模块MyMath.py 103 # print("PI的值为:{:.2f}".format(myMath.PI)) 104 # print("7,22,3,9,12的最大值为:{}".format(myMath.mymax(7,22,3,9,12))) 105 # print("9,77,33的最大值为:{}".format(myMath.mymax(9,77,33))) 106 # ===================================================================== 107 # ''' 108 # 8、现有水果、雪梨、苹果、红苹果、青苹果等几个类, 109 # 水果有美容等功能。 110 # 雪梨除了美容的功能还有止咳的功能, 111 # 苹果除了美容的功能,还有被当成礼品赠送的功能。 112 # 红苹果的颜色是红的,青苹果的颜色是青的, 113 # 并且青苹果虽然有苹果的功能,但是青苹果一般丕用当礼品。 114 # 请根据上面的描述与自然界之间的具体关系,编写程序完成如下功能:u 115 # (1)通过 Python代码模拟出上面的事务与案例。, 116 # (2)通过Python代码判断青苹果的颜色,以及是否有美容的功能、是否有止咳的功能、是否有当礼品的功能。v 117 # ''' 118 # class shuiguo0: 119 # def meirong0(self): 120 # print("美容") 121 # class xueli0(shuiguo0): 122 # def zhike0(self): 123 # print("止咳") 124 # class pingguo0(shuiguo0): 125 # def __init__(self,color): 126 # self.color = color 127 # def gift(self): 128 # print("送礼") 129 # class redapple0(pingguo0): 130 # def __init__(self,color = "红色"): 131 # super().__init__(color) 132 # class greenapple0(pingguo0): 133 # def __init__(self,color = "青色"): 134 # super().__init__(color) 135 # def gift(self): 136 # print("不送礼") 137 # g = greenapple0() 138 # print("{}苹果".format(g.color)) 139 # g.meirong0() 140 # g.gift() 141 # ===================================================================== 142 143 # ===================================================================== 144 # * 号方框 145 # a0 = int(input("")) 146 # if a0 == 1: 147 # print("*") 148 # elif a0 == 0: 149 # print("") 150 # else: 151 # for i in range(1,a0): 152 # if i == 1: 153 # print("* "*a0) 154 # if i == a0-1: 155 # print("* "*a0) 156 # else: 157 # print("* "+" "*(a0-2)*2+"*") 158 # ===================================================================== 159 160 # ===================================================================== 161 # ''' 162 # 一、列表: 163 # 1.创建:alist=[110, "dog", "cat", 120, "apple"] 164 # 2.遍历(访问列表的每一个元素) 165 # 3.添加:append(元素),extend(列表), insert ' 166 # 4.删除:del alist[下标]、pop(index)、remove(元素) 167 # 5.修改:直接给列表元素赋值 168 # 6.查找:index(元素,起始位置,结束为止)7.排序:列表.sort (key,reverse=False) sorted(列表) 169 # ''' 170 # ===================================================================== 171 # li_num1 = [4,5,2,7] 172 # li_num2 = [3,6] 173 # li_num1.extend(li_num2) 174 # li_num1.sort(reverse=True) 175 # print(li_num1) 176 # ===================================================================== 177 # tu_num1 = ('p','y','t',['o','n']) 178 # tu_num1[-1].append("h") 179 # print(tu_num1) 180 # ===================================================================== 181 # ''' 182 # 5、根据要求编写程序:v 183 # (1)创建列表[110, ...dog..., ...cat..,120, ...apple...] 184 # (2)在字符串...dog’..和’.cat’.之间插入空列表 185 # (3)删除列表中”..applem.这个字符串, 186 # (4)分别查找出列表中数值110、120(不能人工数,要采用列表提供的方法)+ 187 # (5)对查找出来的数值110和120执行乘以10的操作运算 188 # (6)打印输出最终的列表 189 # ''' 190 # alist = [110,'dog','cat',120,'apple'] 191 # a0 = alist.index("cat") 192 # alist.insert(a0,[]) 193 # alist.remove("apple") 194 # 195 # for i in range(len(alist)): 196 # if alist[i] == 110: 197 # print(alist[i]) 198 # alist[i] *= 10 199 # if alist[i] == 120: 200 # print(alist[i]) 201 # alist[i] *= 10 202 # 203 # # b0 = alist.index(110) 204 # # c0 = alist.index(120) 205 # # print(alist[b0]) 206 # # print(alist[c0]) 207 # # alist[b0] = alist[b0]*10 208 # # alist[c0] = alist[c0]*10 209 # print(alist) 210 211 # ===================================================================== 212 213 # ===================================================================== 214 # ''' 215 # 3、输入一串字符串作为密码,密码只能由数字、字母与指定的特殊符号组成。编写程序判断输入的密码的强度,并输出,如下所示: 216 # 请输入测试密码(直接回车为退出):wabc123v 217 # abc123的密码强度为2级v' 218 # 请输入测试密码(直接回车为退出):Abc123v 219 # Abc123的密码强度为3级v' 220 # 判定标准如下,满足其中一条,密码强度增加一级: 221 # (1)有“,。! ? ;”特殊符号(2》有大写字母(3)有小写字母(4〉位数不小于8位(5)有数字v 222 # ''' 223 # password = input("请输入一串字符串") 224 # len0 = 0 225 # s0 = 0 226 # dz0 = 0 227 # xz0 = 0 228 # zf0 = 0 229 # class yichang0(Exception): 230 # def __init__(self,i): 231 # self.message = i 232 # if len(password)==0: 233 # print("退出") 234 # else: 235 # try: 236 # if len(password) >= 8: 237 # len0 = 1 238 # for i in password: 239 # if 48 <= ord(i) <= 57: 240 # s0 = 1 241 # elif 65 <= ord(i) <= 90: 242 # dz0 = 1 243 # elif 97 <= ord(i) <= 122: 244 # xz0 = 1 245 # # elif i == '"' or i == "," or i == "." or i == "!" or i == "?" or i == ";" or i == '"': 246 # elif i == "“" or i == "," or i == "。" or i == "!" or i == "?" or i == ";" or i == "”": 247 # zf0 = 1 248 # else: 249 # raise yichang0(i) 250 # except yichang0 as y0: 251 # print(f"{y0.message} 不符合要求") 252 # else: 253 # if len0 == 1 : 254 # print("长度符合") 255 # if s0 == 1 : 256 # print("数字符合") 257 # if dz0 == 1 : 258 # print("大写符合") 259 # if xz0 == 1 : 260 # print("小写符合") 261 # if zf0 == 1 : 262 # print("其他字符符合") 263 # print("密码强度{}级".format(len0 + s0 + dz0 + xz0 + zf0)) 264 # ===================================================================== 265 266 # ===================================================================== 267 # ''' 268 # 字典 269 # ''' 270 # # 1、创建字典 271 # adict = {"中国":"北京","日本":"东京","韩国":"首尔"} 272 # bdict = dict(name = "jack",age = 18,sex = "男") 273 # # 2、调用查找 274 # print(adict["中国"]) 275 # # 3、添加修改 276 # adict["美国"] = "华盛顿" 277 # adict["韩国"] = "汉城" 278 # print(adict) 279 # # 4、删除 280 # # adict.pop("日本") 281 # # adict.popitem() 282 # # adict.clear() 283 # # print(adict) 284 # # 5、遍历 285 # print(adict.items()) 286 # print(adict.keys()) 287 # print(adict.values()) 288 # ===================================================================== 289 # str0 = "skdaskerkjsalkj" 290 # adict = {} 291 # for i in str0: 292 # if i in adict: 293 # adict[i] += 1 294 # else: 295 # adict[i] = 1 296 # print(str0) 297 # print(adict) 298 299 # =====================================================================
#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=
本文来自博客园,作者:凡是过去,皆为序曲,转载请注明原文链接:https://www.cnblogs.com/longhai3/p/15887889.html
如有疑问,欢迎提问
#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=#+=