实验4

 1 print(sum)
 2 
 3 sum=42
 4 
 5 print(sum)
 6 def inc(n):
 7    sum=n+1
 8    print(sum)
 9    return sum
10 sum=inc(7)+inc(7)
11 print(sum)

 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 
 8 
 9 def func2(a, b, c, *, d, e, f):
10      '''
11      返回参数a,b,c,d,e,f构成的列表
12     *后面的参数只能按关键字传递
13      '''
14      return [a, b, c, d, e, f]
15 
16 
17 def func3(a, b, c, /, d, e, f):
18      '''
19      返回参数a,b,c,d,e,f构成的列表
20      /前面的参数只能按位置传递
21       '''
22      return [a, b, c, d, e, f]
23 
24 # func1调用:按位置传递、按参数传递都可以
25 print(func1(a=1, b=9, c=2, d=0, e=5, f=3))
26 print(func1(1, 9, 2, f=3, d=0, e=5))
27 # func2调用:d,e,f必须按关键字传递
28 print(func2(11, 99, 22, d=0, e=55, f=33))
29 print(func2(a=11, b=99, c=22, d=0, e=55, f=33))
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))

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      '''
 3      求解一元二次方程,返回方程的两个根
 4      :param 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 
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()

1 print(solve.__doc__)

 1 def list_generator(x,y,step=1):
 2       list4=[]
 3       while x<=y:
 4           list4.append(x)
 5           x+=step
 6       return list4
 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 def is_prime(x):
 2       if x<=1:
 3           return False
 4       for i in range(2, int(x**0.5)+1):
 5         if x % i==0 :
 6               return False
 7       else:
 8           return True
 9 
10 
11 for y in range(2,21,2):
12      for j in range(2,y):
13          if is_prime(y-j) and is_prime(j):
14              print(f'{y}={j}+{y-j}')

 1 def encoder(s):
 2     ls = []
 3     for i in s:
 4         x = ord(i)
 5         if (x>=97 and x<=117) or (x>=65 and x<=85):
 6             ls.append(chr(x+5))
 7         if (x>=86 and x<=90) or (x>=118 and x<=122):
 8             ls.append(chr(x-21))
 9         if  x<65 or x>122:
10             ls.append(chr(x))
11     return ''.join(ls)
12 def decoder(s):
13     ls = []
14     for i in s:
15         x = ord(i)
16         if (x >= 102 and x <= 122) or (x >= 70 and x <= 90):
17             ls.append(chr(x - 5))
18         if (x >= 65 and x <= 69) or (x >= 97 and x <= 101):
19             ls.append(chr(x+21))
20         if x < 65 or x > 122:
21             ls.append(chr(x))
22     return ''.join(ls)
23 x = input('输入英文文本: ')
24 s = encoder(x)
25 print(f'编码后的文本:{s}')
26 print(f'对编码后的文本解码:{decoder(s)}')

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

 

posted @ 2022-05-10 21:16  是个f个人  阅读(31)  评论(2)    收藏  举报