实验4 函数与异常处理应用编程

task1

 1 print(sum)
 2 sum = 42
 3 print(sum)
 4 
 5 def inc(n):
 6     sum = n+1
 7     print(sum)
 8     return sum
 9 
10 sum = inc(7) + inc(7)
11 print(sum)

 

 不是

line1是全局作用域

line3是全局作用域

line7是局部作用域

line11是全局作用域

 

task2_2

1 list1 = [1, 9, 8, 4]
2 
3 print(sorted(list1))
4 print(sorted(list1, reverse=True))
5 print(sorted(list1, True))

 

 reverse必须使用关键字传递

task2_3

1 def func(a, b, c, /, *, d, e, f):
2     return [a, b, c, d, e, f]
3 
4 print(func(1, 2, 3, d = 4, e = 5, f = 6))

 

 

task3

 1 def solve(a, b, c):
 2     '''
 3     求解一元二次方程, 返回方程的两个根
 4     :para: a,b,c: int 方程系数
 5     :return: tuple
 6     '''
 7     delta = b*b - 4*a*c
 8     delta_sqrt = abs(delta)**0.5
 9     p1 = -b/2/a
10     p2 = delta_sqrt/2/a
11 
12     if delta >= 0:
13         root1 = p1 + p2
14         root2 = p1 - p2
15     else:
16         root1 = complex(p1, p2)
17         root2 = complex(p1, -p2)
18 
19     return root1, root2
20 
21 print(solve.__doc__)
22 while True:
23     try:
24         a,b,c = eval(input('Enter eqution coefficient: '))
25         if a == 0:
26             raise
27     except:
28         print('invalid input, or, a is zero')
29         break
30     else:
31         root1, root2 = solve(a, b, c)
32         print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
33         print()

 

 

task4

 1 def list_generator(x, y, z = 1):
 2     list = []
 3     i = 0
 4     while i <= (y - x):
 5         list.append(x + i)
 6         i += z
 7     return list
 8 
 9 list1 = list_generator(-5, 5)
10 print(list1)
11 
12 list2 = list_generator(-5, 5, 2)
13 print(list2)
14 
15 list3 = list_generator(1, 5, 0.5)
16 print(list3)

 

 

task5

 1 def is_prime(n):
 2     if n == 2:
 3         return True
 4     else:
 5         for i in range(2, n):
 6             if n % i == 0:
 7                 return False
 8         else:
 9             return True
10 
11 list = []
12 for i in range(2,21):
13     if is_prime(i) is True:
14         list.append(i)
15 16 for i in range(2,21,2):
17     try:
18         for j in range(len(list)):
19             for k in range(j,len(list)):
20                 if i == list[j] + list[k]:
21                     print(f'{i} = {list[j]} + {list[k]}')
22                     raise
23     except:
24         pass

 

 

task6

 1 def encoder(x):
 2     x_list = []
 3     for i in range(len(x)):
 4         if ord('a') <= ord(x[i]) <= ord('u') or ord('A') <= ord(x[i]) <= ord('U'):
 5             x_list.append(chr(ord(x[i])+5))
 6         elif ord('v') <= ord(x[i]) <= ord('z') or ord('V') <= ord(x[i]) <= ord('Z'):
 7             x_list.append(chr(ord(x[i])-26+5))
 8         else:
 9             x_list.append(x[i])
10     return ''.join(x_list)
11 
12 def decoder(x):
13     x_list = []
14     for i in range(len(x)):
15         if ord('f') <= ord(x[i]) <= ord('z') or ord('F') <= ord(x[i]) <= ord('Z'):
16             x_list.append(chr(ord(x[i])-5))
17         elif ord('a') <= ord(x[i]) <= ord('e') or ord('A') <= ord(x[i]) <= ord('E'):
18             x_list.append(chr(ord(x[i])+26-5))
19         else:
20             x_list.append(x[i])
21     return ''.join(x_list)
22 
23 x = input('输入英文文本:')
24 x = encoder(x)
25 print(f'编码后的文本:{x}')
26 x = decoder(x)
27 print(f'对编码后的文本解码:{x}')

 

 

task7

 1 def collatz(n):
 2     if n % 2 == 0:
 3         return n/2
 4     elif n % 2 != 0:
 5         return 3*n + 1
 6 
 7 n = eval(input('Enter a positive integer:'))
 8 try:
 9     if type(n) != int:
10         raise
11 except:
12     print('Error:must be a positive integer')
13 else:
14     list = [n]
15     while True:
16         n = int(collatz(n))
17         list.append(n)
18         if n == 1:
19             break
20     print(list)

 

posted @ 2022-05-08 00:50  Kyle-K  阅读(15)  评论(0编辑  收藏  举报