From 15a99c72ec2400a6ed54815abb31fab1dd4b93aa Mon Sep 17 00:00:00 2001 From: wanyi_wang Date: Sun, 3 Jan 2021 17:24:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=5F=E7=AC=AC1?= =?UTF-8?q?=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1.py" | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" new file mode 100644 index 00000000..ad910309 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" @@ -0,0 +1,139 @@ +# 第一问,循环计数 + +# for循环的简单计数 +dict1 = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7} +e1 = " " +i1 = 0 +for e1 in dict1: + i1 += 1 + print(e1) +print(f"final dict number:{i1}") +print("——————————————————————————————————————————————————") + +# while循环-输出30以内的奇数 +j = 0 +i = 2 +while j < 30: + if j % i == 1: + print(int(j)) + j += 1 +print("——————————————————————————————————————————————————") + +# 第二问 +# 100以内斐波那契函数-for +a = 0 +b = 1 +c = 0 +print(a) +print(b) +for i in range(2, 30): + c = a + b + a = b + b = c + if c < 100: + print(c) +print("——————————————————————————————————————————————————") + +# 100以内斐波那契函数-while +d = 0 +e = 1 +f = 0 +print(d) +print(e) +while f < 100: + print(f) + f = d + e + d = e + e = f +print("——————————————————————————————————————————————————") + + +# 第三问,自定义异常paramserror + +class ParamsError(Exception): + pass + + +print('''本计算器可进行以下运算 +加法:运算代码'+' +减法:运算代码'-' +乘法:运算代码'*' +除法:运算代码'/' +整除:运算代码'//' +取余:运算代码'%' +开方:运算代码'1/**' +''') +try: + a = int(input("请输入数值:")) + b = input("请输入运算:") + c = int(input("请输入数值:")) +except ValueError: + raise ParamsError("请检查输入格式") +finally: + print("end") + + +def add(a, c): + return a + c + + +def sub(a, c): + return a - c + + +def mul(a, c): + return a * c + + +def div(a, c): + try: + return a / c + except ZeroDivisionError: + raise ParamsError("除数不可以为0!") + finally: + print("end") + + +def ediv(a, c): + try: + return a // c + except ZeroDivisionError: + raise ParamsError("除数不可以为0!") + finally: + print("end") + + +def rem(a, c): + try: + return a % c + except ZeroDivisionError: + raise ParamsError("取余数不可以为0!") + finally: + print("end") + + +def nthr(a, c): + try: + return a ** (1 / c) + except ZeroDivisionError: + raise ParamsError("开方数不可以为0!") + finally: + print("end") + + +if b == '+': + print("计算结果:", add(a, c)); +elif b == '-': + print("计算结果:", sub(a, c)); +elif b == '*': + print("计算结果:", mul(a, c)); +elif b == '/': + print("计算结果:", div(a, c)); +elif b == '//': + print("计算结果:", ediv(a, c)); +elif b == '%': + print("计算结果:", rem(a, c)); +elif b == '1/**': + print("计算结果:", nthr(a, c)); +else: + print(str("无法计算")) -- Gitee From b4937e6d38d0414350ca6faf3a60dac81f65236a Mon Sep 17 00:00:00 2001 From: wanyi_wang Date: Sun, 3 Jan 2021 22:40:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=5F=E7=AC=AC2?= =?UTF-8?q?=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3-2.py" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2542\350\212\202/3-2.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2542\350\212\202/3-2.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2542\350\212\202/3-2.py" new file mode 100644 index 00000000..69a544ef --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2542\350\212\202/3-2.py" @@ -0,0 +1,46 @@ +classes = [ + {"name": "n_1", "age": 24, "grade": "A"}, + {"name": "n_2", "age": 23, "grade": "B"}, + {"name": "n_3", "age": 28, "grade": "A"}, + {"name": "n_4", "age": 24, "grade": "A"}, + {"name": "n_5", "age": 25, "grade": "C"}, + {"name": "n_6", "age": 21, "grade": "D"}, + {"name": "n_7", "age": 27, "grade": "A"}, +] +from pprint import pprint + +l = classes +# 根据grade来排序 +l.sort(key=lambda x: x["grade"]) +pprint(list(l)) +print("…………………………………………………………………………………………………………") +# 通过filter语句来筛选出Grade为A的同学 +f = filter(lambda x: x["grade"] == "A", l) +pprint(list(f)) +print("…………………………………………………………………………………………………………") + + +# 通过map函数将上述同学的age + 1 +def age_1(a): + a["age"] += 1 + return a + + +m = map(age_1, l) +pprint(list(m)) +print("…………………………………………………………………………………………………………") + + +# 使用递归函数重构100以内的斐波那契函数 +def Fibonacci(n): + if (n == 1): + return 0; + elif (n == 2): + return 1; + else: + return Fibonacci(n - 1) + Fibonacci(n - 2) + + +for i in range(1, 20): + if Fibonacci(i) < 100: + print(Fibonacci(i)) -- Gitee