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

task1.py

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

1.Built-in

2.Global

3.Local

4.Global 

task2_2.py

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

参数reverse的传递方式必须使用关键字传递

task2_3.py

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

task3_1.py

 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     if delta>=0:
12         root1 = p1 + p2
13         root2 = p1 - p2
14     else:
15         root1 = complex(p1, p2)
16         root2 = complex(p1, -p2)
17     return root1, root2
18 
19 while True:
20     try:
21        a,b,c = eval(input('Enter eqution coefficient: '))
22        if a == 0:
23            raise
24     except:
25         print('invalid input, or, a is zero')
26         break
27     else:
28         root1, root2 = solve(a, b, c)
29         print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
30         print()

task3_2.py

 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     if delta>=0:
12         root1 = p1 + p2
13         root2 = p1 - p2
14     else:
15         root1 = complex(p1, p2)
16         root2 = complex(p1, -p2)
17     return root1, root2
18 
19 print(solve.__doc__)
20 while True:
21     try:
22        a,b,c = eval(input('Enter eqution coefficient: '))
23        if a == 0:
24            raise
25     except:
26         print('invalid input, or, a is zero')
27         break
28     else:
29         root1, root2 = solve(a, b, c)
30         print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
31         print()

加一行print(solve.__doc__)会把说明信息打印出来

task4.py

 1 def list_generator(a,b,step=1):
 2     list=[]
 3     while a<=b:
 4         list.append(a)
 5         a+=step
 6     return list
 7 list1 = list_generator(-5, 5)
 8 print(list1)
 9 list2 = list_generator(-5, 5, 2)
10 print(list2)
11 list3 = list_generator(1, 5, 0.5)
12 print(list3)

task5.py

 1 def is_prime(n):
 2     for i in range(2,int(n**0.5)+1):
 3         if n%i==0:
 4             return False
 5     else:
 6         return True
 7 for j in range(2,21,2):
 8     for k in range(2,j):
 9         if is_prime(j-k):
10             print(f'{j}={k}+{j-k}')
11             break

task6.py

 1 def encoder(text:str):
 2     ans=[]
 3     for n in text:
 4         x=ord(n)
 5         if 65<=x<=90:
 6             ans.append(chr((x-60)%26+65))
 7         elif 97<=x<=122:
 8             ans.append(chr((x-92)%26+97))
 9         else:
10             ans.append(n)
11     return ''.join(ans)
12 
13 def decoder(text1):
14     ans1=[]
15     for n in text1:
16         x1=ord(n)
17         if 65 <= x1 <= 90:
18             ans1.append(chr((x1 - 44) % 26 + 65))
19         elif 97 <= x1 <= 122:
20             ans1.append(chr((x1 - 76) % 26 + 97))
21         else:
22             ans1.append(n)
23     return ''.join(ans1)
24 
25 
26 text=input('输入英文文本:')
27 text1=encoder(text)
28 print(f'编码后的文本:{text1}')
29 print(f'解码后的文本:{decoder(text1)}')

task7.py

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

实验总结:python好像越来越难了,代码也越来越复杂了,但是定义好函数之后调用确实很方便,实验异常处理也是非常重要的方式,可以帮助代码更好运行。希望可以学好这一部分,运用到实际问题中去。

posted @ 2022-05-09 23:56  虞渊  阅读(15)  评论(2编辑  收藏  举报