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

一、实验内容

1.实验任务1

task1.py:

 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)

运行截图:

回答:

4处使用的标识符sum不是代表一个变量名。

第一个:内置作用域

第二个:全局作用域

第三个:局部作用域

第四个:全局作用域

 

2.实验任务2

task2_1.py:

 1 def func1(a,b,c,d,e,f):
 2     '''返回参数a,b,c,d,e,f构成的列表
 3     默认,参数按位置传递;也支持关键字传递
 4     '''
 5     return [a,b,c,d,e,f]
 6 
 7 def func2(a,b,c,*,d,e,f):
 8     '''返回参数a,b,c,d,e,f构成的列表
 9     *后面的参数只能按关键字传递
10     '''
11     return [a,b,c,d,e,f]
12 
13 def func3(a,b,c,/,d,e,f):
14     '''返回参数a,b,c,d,e,f构成的列表
15     /前面的参数只能按位置传递'''
16     return[a,b,c,d,e,f]
17 
18 print(func1(1,9,2,0,5,3))
19 print(func1(a=1,b=9,c=2,d=0,e=5,f=3))
20 print(func1(1,9,2,f=3,d=0,e=5))
21 
22 print(func2(11,99,22,d=0,e=55,f=33))
23 print(func2(a=11,b=99,c=22,d=0,e=55,f=33))
24 print(func2(11,99,22,0,55,33))

说明 func2函数中d,e,f 需要用关键字参数传递。

task2_2.py:

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

运行截图:

回答:python内置函数sorted()中,参数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)) 

运行截图:

 

3.实验任务3

task3.py:

 1 def solve(a,b,c):
 2     '''求解一元二次方程,返回方程的两个根
 3     :para:a,b,c:int 方程系数
 4     :return:tuple
 5     '''
 6     delta=b*b-4*a*c
 7     delta_sqrt=abs(delta)**0.5
 8     p1=-b/2/a
 9     p2=delta_sqrt/2/a
10     if delta>0:
11         root1=p1+p2
12         root2=p1-p2
13     else:
14         root1 = complex(p1, p2)
15         root2 = complex(p1, -p2)
16     return root1,root2
17 print(solve.__doc__)
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()

运行截图:

 

4.实验任务4

task4.py:

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

运行截图:

 

5.实验任务5

task5.py:

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

运行截图:

 

6.实验任务6

task6.py:

 1 def  encoder(x):
 2     a=[]
 3     for i in x:
 4         b=ord(i)
 5         if 97<=b<=117 or 65<=b<=85:
 6             b+=5
 7         elif 118<=b<=122 or 86<=b<90:
 8             b=b-21
 9         a.append(chr(b))
10     return ''.join(a)
11 def   decoder(x):
12     a=[]
13     for i in x:
14         b=ord(i)
15         if 102<=b<=122 or 70<=b<=90:
16             b-=5
17         elif  97<=b<=101 or 65<=b<=69:
18             b=b+21
19         a.append(chr(b))
20     return ''.join(a)
21 x=input('输入英文文本: ')
22 m=encoder(x)
23 n=decoder(m)
24 print(f'编码后的文本:{m}')
25 print(f'对编码后的文本解码:{n}')

运行截图:

 

7.实验任务7

task7.py:

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

运行截图:

 

二、实验总结

通过本次实验任务我对ord()与chr()函数更加熟练运用了,也能正确定义和使用函数,但编写的代码比较繁杂,课后应多练习,简化代码。

 

posted @ 2022-05-05 11:22  Dimple$  阅读(12)  评论(2编辑  收藏  举报