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

1,实验任务1

 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)

运行结果:

问题:

不是,1,内置作用域;2,全域作用域;3,局部作用域,4;全域作用域

2,实验任务2

task2_2.py:

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

运行结果:

必须使用关键字传递

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

 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 print(solve.__doc__)
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

 1 def list_generator(x,y,z=1):
 2     a=[]
 3     while x<=y:
 4         a.append(x)
 5         x+=z
 6     return a
 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)

运行结果:

5,实验任务5

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

运行结果:

6,实验任务6

 1 def encoder(z):
 2     a = list(z)
 3     i = 0
 4     while i < len(z):
 5         x = ord(a[i])
 6         if 65 <= x <= 85 or 97 <= x <= 117:
 7             y = chr(x + 5)
 8         elif 85 < x <= 90 or 117 < x <= 122:
 9             y = chr(x - 21)
10         else:
11             y = chr(x)
12         a[i]=y
13         i += 1
14     return ''.join(a)
15 def decoder(z):
16     a = list(z)
17     i = 0
18     while i < len(z):
19         x = ord(a[i])
20         if 70 <= x <= 90 or 102 <= x <= 122:
21             y = chr(x - 5)
22         elif 65 <= x < 70 or 97 <= x < 102:
23             y = chr(x + 21)
24         else:
25             y = chr(x)
26         a[i]=y
27         i += 1
28     return ''.join(a)
29 z = input('输入英文文本:')
30 print(f'''编码后的文本:{encoder(z)}
31 对编码后的文本解码:{decoder(encoder(z))}''')

运行结果:

7,实验任务7

def collatz(n):
    if n%2==0:
        return int(n/2)
    else:
        return int(3*n+1)
try:
    n=eval(input('Enter a positive integer:'))
    if n<=0 or type(n)!=int:
        raise
except:
    print('Error: must be a positive integer')
else:
    x=[]
    while n!=1:
        x.append(n)
        n=collatz(n)
    x.append(n)
    print(x)

运行结果:

 

posted @ 2022-05-08 16:39  炸呼  阅读(12)  评论(2编辑  收藏  举报