1.实验任务1
print(sum) sum = 42 print(sum) def inc(n): sum = n + 1 print(sum) return sum sum = inc(7) + inc(7) print(sum)
问题:不是。line1中的sum是指Python的内置函数;line3中的sum指的是line2中的全局变量sum;line7中的sum指的是inc函数中的局部变量;line11中的sum指的是line10的全局变量
2.实验任务2
task1
def func1(a, b, c, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 默认,参数按位置传递; 也支持关键字传递 ''' return [a,b,c,d,e,f] def func2(a, b, c,*, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 *后面的参数只能按关键字传递 ''' return [a,b,c,d,e,f] def func3(a, b, c, /, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 /前面的参数只能按位置传递 ''' return [a,b,c,d,e,f] # func1调用:按位置传递、按参数传递都可以 print( func1(1,9,2,0,5,3) ) print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) print( func1(1,9,2, f=3, d=0, e=5)) # func2调用:d,e,f必须按关键字传递 print( func2(11, 99, 22, d=0, e=55, f=33) ) print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) # func3调用:a,b,c必须按位置传递 print( func3(111, 999, 222, 0, 555, 333)) print( func3(111, 999, 222, d=0, e=555, f=333) )
运行结果如图:
增加print( func2(11, 99, 22, 0, 55, 33))后:
增加print(func3(a=111, b=999, c=222, 0, 555, 333))后:
task2_2
list1 = [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
运行结果如图:
task2_2所示情况中,必须使用关键字传递reversse
task2_3
1 def func(a, b, c, /, *, d, e, f): 2 return( [a,b,c,d,e,f] ) 3 4 5 print(func(1,2,3,d = 4,e = 5,f = 6))
3.实验任务3
def solve(a, b, c): ''' 求解一元二次方程, 返回方程的两个根 :para: a,b,c: float 方程系数 :return: tuple ''' delta = b*b - 4*a*c delta_sqrt = abs(delta)**0.5 p1 = -b/2/a p2 = delta_sqrt/2/a if delta >= 0: root1 = p1 + p2 root2 = p1 - p2 else: root1 = complex(p1, p2) root2 = complex(p1, -p2) return root1, root2 while True: try: t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') if t == '#': print('结束计算,退出') break a, b, c = map(float, t.split()) if a == 0: raise ValueError('a = 0, 不是一元二次方程') except ValueError as e: print(repr(e)) print() except: print('有其它错误发生\n') else: root1, root2 = solve(a, b, c) print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') print()
运行结果如图:
加了print(solve.__doc__)后:
4.实验任务4
def list_generator(start,end,step=1): lst = [] while start <= end: lst.append(start) start = start + step print(lst) list_generator(-5,5) list_generator(-5,5,2) list_generator(1,5,0.5)
运行结果如图:
5.实验任务5
def is_prime(n): for i in range(2,n+1): if n % i == 0 or n == 2: return False else: return True prime_list = [] for n in range(2,21): if is_prime(n) == True: prime_list.append(n) else: pass i = 2 while i <= 20: j = 0 while j < len(prime_list): k = 0 while k <len(prime_list): if prime_list[j] + prime_list[k] == i: print(f'{i} = {prime_list[j]} + {prime_list[k]}') else: pass k += 1 break # 找到后停止该循环 j += 1 i += 2
运行结果如图:
6.实验任务6
# 编码函数encoder()定义 def encoder(text): text_list = list(text) for i in range(len(text_list)): if ord('a') <= ord(text_list[i]) <= ord('z'): add5 = ord(text_list[i]) + 5 if add5 <= ord('z'): text_list[i] = chr(add5) else: add5 = add5 % ord('z') - 1 +ord('a') text_list[i] = chr(add5) elif ord('A') <= ord(text_list[i]) <= ord('Z'): add5 = ord(text_list[i]) + 5 if add5 <= ord('z'): text_list[i] = chr(add5) else: add5 = add5 % ord('Z') - 1 +ord('A') text_list[i] = chr(add5) else: pass return ''.join(text_list) # 解码函数decoder()定义 def decoder(text): text_list = list(text) for i in range(len(text_list)): if ord('a') <= ord(text_list[i]) <= ord('z'): red5 = ord(text_list[i]) - 5 if ord('a') <= red5: text_list[i] = chr(red5) else: red5 = ord('z') - (ord('a') - red5 - 1) text_list[i] = chr(red5) elif ord('A') <= ord(text_list[i]) <= ord('Z'): red5 = ord(text_list[i]) - 5 if ord('A') <= red5: text_list[i] = chr(red5) else: red5 = ord('Z') - (ord('A') - red5 - 1) text_list[i] = chr(red5) else: pass return ''.join(text_list) # 主体代码逻辑 text = input('输入英文文本: ') encoded_text = encoder(text) print('编码后的文本: ', encoded_text) decoded_text = decoder(encoded_text) print('对编码后的文本解码: ', decoded_text)
运行结果如图:
7.实验任务7
# collatz函数定义 def collatz(n): n_list.append(n) while True: if n % 2 == 0 and n != 0: collatz(n // 2) elif (n + 1) % 2 == 0 and n != 1: collatz(n * 3 + 1) elif n == 1: print(n_list) break # 自定义异常 class Error(Exception): def __init__(self,n): self.n = n def __str__(self): print('Error: must be a positive integer') # 主题代码逻辑 try: n_list = [] n = input('输入一个正整数:') if n.isdigit() == False: raise Error(n) elif int(n) <= 0: raise Error(n) else: collatz(int(n)) except Error: print('Error: must be a positive integer')
8.实验任务8
# 函数func()定义 def func(n): if n == 1: ans = 2 - 1 else: ans = 2 * func(n-1) +1 n -= 1 return ans # while True: x = input() if x == '#': print('计算结束') break n = int(x) ans = func(n) print(f'n = {n}, ans = {ans}')
运行结果如图:
实验总结
作用域:LEGB(局部-嵌套-全句-内置)
参数: * 后面的只能按关键字传递
/前面的只能按位置传递
函数的异常和处理
嵌套:找出f(n)和f(n-1)的关系
标签:return,函数,sum,list,实验,print,处理程序,True,def From: https://www.cnblogs.com/bhsyyds/p/17426238.html