实验四

实验任务一

print(sum)
sum = 42
print(sum)


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


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

1.Built-in

2.Global

3.Local

4.Global 

实验任务2

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

1、是

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

实验任务3

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

实验任务4

def list_generator(begin, end, step=1):
    """生成特定范围区间"""
    ls = []
    a = begin
    b = end
    while a <= b:
        ls.append(a)
        a += step
    return ls


list1 = list_generator(-5, 5)
print(list1)
list2 = list_generator(-5, 5, 2)
print(list2)
list3 = list_generator(1, 5, 0.5)
print(list3)

实验任务5

 

def is_prime(a):
    """判断是否是素数"""
  for i in range(2, int(a)+1):
    if not n%i:
      return False  
    else:
      return True


ls = []
for i in range(20):
    if is_prime(i):
        ls.append(i)


def summary(x):
    """验证歌德巴赫猜想"""
    i = 0
    while True:
        for j in range(i, len(ls)):
            s = ls[i]+ls[j]
            if x == s:
                s = f"{s} = {ls[i]} + {ls[j]}"
                return s
        i += 1


for i in range(4, 21, 2):
    print(summary(i))

实验任务6

 

def encoder(a):
    """对文本加密"""
    result = ''
    for i in range(len(a)):
        s = ord(a[i]) + 5
        if ord(a[i]) in range(65, 91) or ord(a[i]) in range(97, 123):
            if s in range(65, 91) or s in range(97, 123):
                result += chr(s)
            else:
                s = ord(a[i]) - 21
                result += chr(s)
        else:
            result += a[i]
    return result


def decoder(a):
    """解码加密文本"""
    rt = ''
    for i in range(len(a)):
        s = ord(a[i]) - 5            
        if ord(a[i]) in range(65, 91) or ord(a[i]) in range(97, 123):
            if s in range(65, 91) or s in range(97, 123):
                rt += chr(s)
            else:
                s = 21 + ord(a[i])
                rt += chr(s)
        else:
            rt += a[i]
    return rt


text = input("输入英文文本: ")
print(f"编码后的文本: {encoder(text)}")
print(f"对编码后的文本解码: {decoder(encoder(text))}")    

实验任务7

def collatz(a):
    """验证Collatz猜想"""
    if not a % 2:
        return a / 2
    else:
        return 3 * a + 1


try:
    n = int(input("Enter a positive integer: "))
    if n <= 0:
        raise ValueError
    ls = [n]
    while n != 1:
        n = collatz(n)
        ls.append(n)
    print([int('{:.0f}'.format(i)) for i in ls])
except ValueError:
    print("Error:must be a positive integer")

实验总结

1、差点被气死

2、替换字符串的时候可以考虑创建一个空字符串

posted @ 2022-05-04 22:36  杖卫  阅读(44)  评论(1编辑  收藏  举报