实验四

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)

答:不是代表一个变量名。

       1.Built-in   2.Global   3.Local   4.Global

 

task2_2:

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

答:必须使用关键字传递。

 

task2_3:

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

 

task3

 1 def solve(a, b, c):
 2     ''' 求解一元二次方程, 返回方程的两个根
 3     :para: a,b,c: int 方程系数
 4     :return: tuple '''
 5     delta = b*b - 4*a*c
 6     delta_sqrt = abs(delta)**0.5
 7     p1 = -b/2/a;
 8     p2 = delta_sqrt/2/a
 9 
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 
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()

 

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 False
 4     else:
 5         for i in range(2,n):
 6             if n%i==0:
 7                 return False
 8                 break
 9         else:
10             return True
11 
12 num=[]
13 for i in range(20):
14     if is_prime(i):
15         num.append(i)
16 
17 def summary(x):
18     i = 0
19     while True:
20         for j in range(i, len(num)):
21             s = num[i]+num[j]
22             if x == s:
23                 s = f"{s} = {num[i]} + {num[j]}"
24                 return s
25         i += 1
26 
27 
28 for i in range(4, 21, 2):
29     print(summary(i))

 

 

task6

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

 

task7

 1 def collatz(n):
 2     if n%2==0:
 3         n=int(n/2)
 4         return n
 5     else:
 6         n=n*3+1
 7         return n
 8 try:
 9     n=eval(input('输入一个正整数:'))
10     if n<=0 or type(n) is not int:
11         raise
12 except:
13     print('Error:must be a positive inreger')
14 
15 else:
16     list=[]
17     while n!=1:
18         list.append(n)
19         n=collatz(n)
20     list.append(1)
21     print(list)

 

posted on 2022-05-11 00:12  蒜头小兲  阅读(8)  评论(1编辑  收藏  举报

导航