实验4

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

四行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 
25 # func1调用:按位置传递、按参数传递都可以
26 print(func1(1, 9, 2, 0, 5, 3))
27 print(func1(a=1, b=9, c=2, d=0, e=5, f=3))
28 print(func1(1, 9, 2, f=3, d=0, e=5))
29 # func2调用:d,e,f必须按关键字传递
30 print(func2(11, 99, 22, d=0, e=55, f=33))
31 print(func2(a=11, b=99, c=22, d=0, e=55, f=33))
32 # func3调用:a,b,c必须按位置传递
33 print(func3(111, 999, 222, 0, 555, 333))
34 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 
4 
5 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 while True:
19     try:
20         a,b,c = eval(input('Enter eqution coefficient: '))
21         if a == 0:
22             raise
23     except:
24         print('invalid input, or, a is zero')
25         break
26     else:
27         root1, root2 = solve(a, b, c)
28         print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
29         print()

在第二十二行前面添加一行代码

1 print(solve.__doc__) 

 

 

 1 def list_generator(x, y, step=1):
 2     list6 = []
 3     while x <= y:
 4         list6.append(x)
 5         x += step
 6     return list6
 7 
 8 
 9 list1 = list_generator(-5, 5)
10 print(list1)
11 list2 = list_generator(-5, 5, 2)
12 print(list2)
13 list3 = list_generator(1, 5, 0.5)
14 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}')

 

def encoder(x):
    list1 = []
    for i in x:
        i = ord(i)
        if 97 <= i <= 117 or 65 <= i <= 85:
            i += 5
        elif 117 < i <= 122 or 85 < i <= 90:
            i -= 21
        else:
            i = i
        list1.append(chr(i))
    return ''.join(list1)


def decoder(x):
    x = encoder(x)
    list2 = []
    for i in x:
        i = ord(i)
        if 102 <= i <= 122 or 70 <= i <= 90:
            i -= 5
        elif 97 <= i < 102 or 65 <= i < 70:
            i += 21
        else:
            i = i
        list2.append(chr(i))
    return ''.join(list2)


x = input('输入英文字符串:')
print(f'编码后{encoder(x)}')
print(f'解码后{decoder(x)}')

 1 def mypow(x):
 2     if x % 2 == 0:
 3         return x / 2
 4     else:
 5         return x * 3 + 1
 6 
 7 
 8 while True:
 9     try:
10         x = eval(input('输入一个数:'))
11         if int(x) != x or x <= 0:
12             raise ValueError and NameError
13     except ValueError and NameError:
14         print('must be a positive integer')
15     else:
16         list = [x]
17         while x != 1:
18             x = mypow(x)
19             list.append(x)
20         print(list)

不会啊不会

posted @ 2022-05-04 23:22  气工吴旭  阅读(19)  评论(3编辑  收藏  举报