4.1

print(sum)
sum = 42
print(sum)

def inc(n):
    sum = n+1
    print(sum)
    return sum
sum = inc(7) + inc(7)
print(sum)

不是

line1:内置函数

line3:赋值给予的数

line7:局部变量

line11:全局变量

 

4.2.2

list1 = [1, 9, 8, 4]

print( sorted(list1) )
print( sorted(list1, reverse=True) )
print( sorted(list1, True) )

参数reverse的传递方式必须使用关键字传递
 
4.2.3
def func(a, b, c, /, *, d, e, f):
    return( [a,b,c,d,e,f] )
print(func(1, 2, 3, d=4, e=5, f=6))

 

4.3

def solve(a, b, c):
   delta = b*b - 4*a*c
    delta_sqrt = abs(delta)**0.5
    p1 = -b/2/a;
    p2 = delta_sqrt/2/a
    if delta>=0:
        root1 = p1 + p2
        root2 = p1 - p2
    else:
        root1 = complex(p1, p2)
        root2 = complex(p1, -p2)
    return root1, root2

while True:
    try:
        a,b,c = eval(input('Enter eqution coefficient: '))
        if a == 0:
            raise
    except:
        print('invalid input, or, a is zero')
        break
    else:
        root1, root2 = solve(a, b, c)
        print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
        print(

 

 

 

def solve(a, b, c):
    ''' 求解一元二次方程, 返回方程的两个根
    :para: a,b,c: int 方程系数
    :return: tuple
    '''
    delta = b*b - 4*a*c
    delta_sqrt = abs(delta)**0.5
    p1 = -b/2/a;
    p2 = delta_sqrt/2/a
    if delta>=0:
        root1 = p1 + p2
        root2 = p1 - p2
    else:
        root1 = complex(p1, p2)
        root2 = complex(p1, -p2)
    return root1, root2

print(solve.__doc__)
while True:
    try:
        a,b,c = eval(input('Enter eqution coefficient: '))
        if a == 0:
            raise
    except:
        print('invalid input, or, a is zero')
        break
    else:
        root1, root2 = solve(a, b, c)
        print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
        print()

 

输入了line3-5的内容

 

4.4

def list_generator(a,b,step=1):
    list_g=[]
    while a<=b:
        list_g.append(a)
        a=a+step
    return list_g

list1 = list_generator(-5, 5)
print(list1)

list2 = list_generator(-5, 5, 2)
print(list2)

list3 = list_generator(1, 5, 0.5)
print(list3)

 

4.5

def isPrime(n):
    for i in range(1,n+1):
        a=[]
        for j in range(2,i+1):
            if i%j==0:
                a.append(j)
    if len(a)==1:
        return True
    else:
        return False

b=[]
for i in range(1,21):
    if isPrime(i)==True:
        b.append(i)


for j in range(4,21,2):
    for x in b:
        for y in b:
            if j==x+y and x<=y:
                print(f'{j}={x}+{y}')
                break
            else:
                continue

 

4.6

def encoder(n):
    a= [chr(i) for i in range(97, 123)]
    b= [chr(i) for i in range(65, 91)]
    c=[]
    for i in n:
            if i not in a and b:
                c.append(i)
            elif i in a:
                x=a.index(i)
                if x>20:
                    c.append(a[x-21])
                else:
                    c.append(a[x+5])
            elif i in b:
                y=b.index(i)
                if y>20:
                    c.append(a[y-21])
                else:
                    c.append(a[y+5])
    d=''.join(c)
    return d

def decoder(n):
    a= [chr(i) for i in range(97, 123)]
    b= [chr(i) for i in range(65, 91)]
    c=[]
    for i in n:
            if i not in a and b:
                c.append(i)
            elif i in a:
                x=a.index(i)
                if x<5:
                    c.append(a[x+21])
                else:
                    c.append(a[x-5])
            elif i in b:
                y=b.index(i)
                if y<5:
                    c.append(a[y+21])
                else:
                    c.append(a[y-5])
    d=''.join(c)
    return d

n=input('输入英文文本:')
print('编码后的文本:'+encoder(n))
print('对编码的文本解码:'+decoder(encoder(n)))

 

 

4.7

a=[]
def Collatz(n):
    if n==1:
        a.append(1)
        return a
    if n%2==0:
        a.append(int(n))
        return Collatz(n/2)
    if n%2!=0:
        a.append(int(n))
        return Collatz(3*n+1)

try:
    x=int(input('Enter a positive integer:'))
    if abs(x)+x==0 or type(x)!=int:
        raise
except:
    print('Error: must be a positive integer')
else:
    print(Collatz(x))

 

 

 实验总结

1.return的值要把握好

2.定义函数注意循环

 

 

 

 

posted on 2022-05-07 16:07  花二  阅读(33)  评论(1编辑  收藏  举报