实验四

 

 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 sum = inc(7) + inc(7)
10 print(sum)

回答:

不是;line2,6是全局作用域,9是函数作用域

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

reverse需要参数传递

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

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

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

 1 from math import sqrt
 2 def is_prime(x):
 3     if x<2:
 4         return False
 5     else:
 6         for i in range(2,int(sqrt(x)+1)):
 7             if x%i==0:
 8                 return False
 9         else:
10             return True
11 
12 def gede(x):
13     for i in range(2,x):
14         h=x-i
15         if is_prime(h) and is_prime(i):
16             print(x,"=",i,"+",h)
17             return True
18     return False
19 i=4
20 while(gede(i) and i<20):
21     i+=2

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

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

 收获:

1,复习了字母大小写的转换方法,以及定义函数的调用

2,学会了处理程序报错的方法,并且耐心得到了锻炼

posted @ 2022-05-09 23:46  爱冒险的小洋  阅读(26)  评论(2编辑  收藏  举报