# 实验四

 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：全局

 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 def func2(a, b, c,*, d, e, f):
7     '''
8     返回参数a,b,c,d,e,f构成的列表
9     *后面的参数只能按关键字传递
10     '''
11     return [a,b,c,d,e,f]
12 def func3(a, b, c, /, d, e, f):
13     '''
14     返回参数a,b,c,d,e,f构成的列表
15     /前面的参数只能按位置传递
16     '''
17     return [a,b,c,d,e,f]
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
25 print( func3(111, 999, 222, 0, 555, 333))
26 print( func3(111, 999, 222, d=0, e=555, f=333) )

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))

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

 1 def list_generator (begin,end,step=1):
2     list=[]
3     while begin<=end:
4         list.append(begin)
5         begin=begin+step
6     return list
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)

 1 def is_prime(n):
2     if n < 2:
3         return False
4     for i in range(2,int(n**0.5)+1):
5         if n % i == 0:
6             return False
7     return True
8
9 h=[i for i in range(21)]
10 for x in h[::2]:
11     for a in range(0,21):
12         if is_prime(a) is True:
13             if is_prime(x-a) is True:
14                 print(f'{x}={a}+{x-a}')
15                 break
16             else:
17                 a+=1
18         else:
19             a+=1

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

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

posted @ 2022-05-10 14:56  pray4hell  阅读(8)  评论(3编辑  收藏  举报