实验四

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

 

 

 不是,依次为内置作用域,作用于闭包函数外的函数中的作用域(Enclosing,简写为 E),局部作用域,全局作用域

 

 

2—1

 1 def func1(a, b, c, d, e, f):
 2     '''
 3     返回参数a,b,c,d,e,f构成的列表
 4     默认,参数按位置传递; 也支持关键字传递
 5     '''
 6     return [a,b,c,d,e,f]
 7 def func2(a, b, c,*, d, e, f):
 8     '''
 9     返回参数a,b,c,d,e,f构成的列表
10     *后面的参数只能按关键字传递
11     '''
12     return [a,b,c,d,e,f]
13 def func3(a, b, c, /, d, e, f):
14     '''
15     返回参数a,b,c,d,e,f构成的列表
16     /前面的参数只能按位置传递
17     '''
18     return [a,b,c,d,e,f]
19 
20 # func1调用:按位置传递、按参数传递都可以
21 print( func1(1,9,2,0,5,3) )
22 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) )
23 print( func1(1,9,2, f=3, d=0, e=5))
24 # func2调用:d,e,f必须按关键字传递
25 print( func2(11, 99, 22, d=0, e=55, f=33) )
26 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) )
27 
28 print( func2(11, 99, 22, 0, 55, 33) )
29 
30 # func3调用:a,b,c必须按位置传递
31 print( func3(111, 999, 222, 0, 555, 333))
32 print( func3(111, 999, 222, d=0, e=555, f=333) )

 

 

 

 

 

 reserve以key判断

 

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

 

 

 

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     delta = b*b - 4*a*c
 3     delta_sqrt = abs(delta)**0.5
 4     p1 = -b/2/a
 5     p2 = delta_sqrt/2/a
 6 
 7     if delta >= 0:
 8         root1 = p1 + p2
 9         root2 = p1 - p2
10     else:
11         root1 = complex(p1, p2)
12         root2 = complex(p1, -p2)
13 
14     return root1, root2
15 
16 print(solve.__doc__)
17 while True:
18     try:
19         a,b,c = eval(input('Enter eqution coefficient: '))
20         if a == 0:
21             raise
22     except:
23         print('invalid input, or, a is zero')
24         break
25     else:
26         root1, root2 = solve(a, b, c)
27         print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
28         print()

 

 

 

 1 def list_generator(a,b,c=1):
 2     l=[a]
 3     while True:
 4         if c==None:
 5             a+=1
 6             l.append(a)
 7             if a==b:
 8                 break
 9         else:
10             a+=c
11             l.append(a)
12             if a==b:
13                 break
14     return l
15 list1 = list_generator(-5,5)
16 print(list1)
17   
18 list2 = list_generator(-5,5,2)
19 print(list2)
20   
21 list3 = list_generator(1,5,0.5)
22 print(list3)

 

 

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

 

 

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

 

 

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

 

posted @ 2022-05-10 21:36  lsqb27  阅读(5)  评论(2编辑  收藏  举报