其他分享
首页 > 其他分享> > 实验四

实验四

作者:互联网

Task1:

 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)
10 #不是;line1、3、11是全局作用域。line7是函数作用域。

不代表一个变量名;line1、3、11是全局作用域。line7是函数作用域。

Task2_2:

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

是, 需要,理性的,中肯的,正确的,一针见血了。

Task2_3:

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

Task3:

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

Task4:

 1 # list_generator()函数定义
 2 # 待补足
 3 # ×××
 4 def list_generator(a, b, c = 1):
 5     list = []
 6     n = a
 7     while(n <= 5):
 8         list.append(n)
 9         n = n + c
10     return list
11 
12 
13 list1 = list_generator(-5, 5)
14 print(list1)
15 
16 list2 = list_generator(-5, 5, 2)
17 print(list2)
18 
19 list3 = list_generator(1, 5, 0.5)
20 print(list3)

Task5:

 1 def isPrime(x):
 2     if(x == 1):
 3         return False
 4     else:
 5         for i in range(2,x):
 6             if(x % i == 0):
 7                 return False
 8                 break
 9         else:
10             return True
11 
12 oushu = [i for i in range(4, 2100, 2)]
13 sushu = []
14 for i in range(1, max(oushu)):
15 
16     if(isPrime(i) == True):
17         sushu.append(i)
18 
19 for n in oushu:
20     for i in range(len(sushu)):
21         for j in range(len(sushu)):
22             if(n == sushu[i] + sushu[j]):
23                 print('{} = {} + {}'.format(n, sushu[i], sushu[j]))
24                 break
25         if(n == sushu[i] + sushu[j]):
26             break

Task6:

 1 def encoder(x):
 2     x = list(x)
 3     for i in range(len(x)):
 4 
 5         if('z' >= x[i].lower() >= 'v'):
 6             x[i] = chr(ord(x[i]) - 21)
 7         elif('v' > x[i].lower() >= 'a'):
 8             x[i] = chr(ord(x[i]) + 5)
 9 
10     x = ''.join(x)
11     return x
12 
13 def decoder(x):
14     x = list(x)
15     for i in range(len(x)):
16 
17         if('a' <= x[i].lower() <= 'e'):
18             x[i] = chr(ord(x[i]) + 21)
19         elif('e' < x[i].lower() <= 'z'):
20             x[i] = chr(ord(x[i]) - 5)
21 
22     x = ''.join(x)
23     return x
24 
25 wenben = input('输入英文文本;')
26 print('编码后的文本:{}'.format(encoder(wenben)))
27 print('对编码后的文本解码:{}'.format(decoder(encoder(wenben))))

Task7:

 1 def collatz(x):
 2     if(x % 2 == 0):
 3         x = int(x / 2)
 4     else:
 5         x = 3 * x + 1
 6     return x
 7 
 8 list = []
 9 
10 try:
11     n = eval(input('Enter a positive integer: '))
12 
13     if(n <= 0):
14         raise
15     elif(int(n) != n):
16         raise
17 
18     while True:
19         if(n == 1):
20             list.append(n)
21             break
22         else:
23             list.append(int(n))
24             n = collatz(n)
25 
26     print(list)
27 
28 except:
29     print('Error: must be a positive integer')

 

 

总结:

1.实验很棒,我很喜欢。

标签:return,sum,range,实验,sushu,print,def
来源: https://www.cnblogs.com/Cr2O3/p/16228349.html