实验四

task1.py
print(sum)
sum=42
print(sum)

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

sum=inc(7)+inc(7)
print(sum)

task2_2.py

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

 

 task2_3.py

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

 

 task3.py

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

 

 

 

 task4.py

def list_generator(a,b,c=1):
    ls = []
    s = a
    if c >= 1:
        for i in range(a,b+1,c):
            ls.append(i)
    if c < 1:
        for j in range(a,b+1):
            ls.append(j)
            ls.append(j+c)
    return ls
lsit1 = list_generator(-5,5)
print(lsit1)

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

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

 

 task5.py

def is_prime(a):
    x = 0
    for i in range(1,a+1):
        if a%i == 0:
            x+=1
    if x == 2:
        return True
    else:
        return False
ls = []
for i in range(1,21):
    if is_prime(i):
        ls.append(i)
lk = ls.copy()
for j in range(2,21,2):
    for m in ls:
        for n in lk:
            if j == n+m:
                print(f'{j} = {m}+{n}')

 

 task6.py

def encoder(s):
    ls = []
    for i in s:
        x = ord(i)
        if (x>=97 and x<=117) or (x>=65 and x<=85):
            ls.append(chr(x+5))
        if (x>=86 and x<=90) or (x>=118 and x<=122):
            ls.append(chr(x-21))
        if  x<65 or x>122:
            ls.append(chr(x))
    return ''.join(ls)
def decoder(s):
    ls = []
    for i in s:
        x = ord(i)
        if (x >= 102 and x <= 122) or (x >= 70 and x <= 90):
            ls.append(chr(x - 5))
        if (x >= 65 and x <= 69) or (x >= 97 and x <= 101):
            ls.append(chr(x+21))
        if x < 65 or x > 122:
            ls.append(chr(x))
    return ''.join(ls)
x = input('输入英文文本: ')
s = encoder(x)
print(f'编码后的文本:{s}')
print(f'对编码后的文本解码:{decoder(s)}')

 

 task7.py

def collatz(n):
    if n%2 == 0:
        return n/2
    elif n%2!=0:
        return n*3+1
while True:
    l = []
    try:
        x = eval(input('enter a positive integer:'))
        if type(x) is not int or x <= 0:
            raise NameError
    except NameError:
        print('Error:must be a positive integer')
    else:
        while x>1:
            l.append(x)
            x = collatz(x)
        l.append(x)
        print(l)

 

posted on 2022-05-09 22:53  不太妙哈  阅读(25)  评论(2编辑  收藏  举报