SAKURA-QiQi

导航

实验四

task1
Created on Mon May  9 19:12:43 2022

@author: ASUS
"""

print(sum)
sum = 42
print(sum)

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

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

 

 不是.

line1的sum作用域为line1,line3作用域为line2,line7作用域为line6,line11作用域为line10

task2_2

Created on Mon May  9 19:26:12 2022

@author: ASUS
"""

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

 

 

task2_3

"""
Created on Mon May  9 19:30:35 2022

@author: ASUS
"""
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

"""
Created on Mon May  9 19:45:33 2022

@author: ASUS
"""

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

"""
Created on Mon May  9 20:00:57 2022

@author: ASUS
"""

def list_generator(x, y, z = 1):
    list = []
    i = 0
    while i <= (y - x):
        list.append(x + i)
        i += z
    return list

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

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

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

 

 task5

Created on Mon May  9 20:20:15 2022

@author: ASUS
"""

def is_prime(n):
     if n == 2:
         return True
     else:
         for i in range(2, n):
             if n % i == 0:
                 return False
         else:
             return True 
list = []
for i in range(2,21):
     if is_prime(i) is True:
         list.append(i)
for i in range(2,21,2):
    try:
        for j in range(len(list)):
            for k in range(j,len(list)):
                if i == list[j] + list[k]:
                     print(f'{i} = {list[j]} + {list[k]}')
                     raise
    except:
         pass

 

 task6

# -*- coding: utf-8 -*-
"""
Created on Mon May  9 20:42:50 2022

@author: ASUS
"""

def encoder(x):
    x_list = []
    for i in range(len(x)):
        if ord('a') <= ord(x[i]) <= ord('u') or ord('A') <= ord(x[i]) <= ord('U'):
            x_list.append(chr(ord(x[i])+5))
        elif ord('v') <= ord(x[i]) <= ord('z') or ord('V') <= ord(x[i]) <= ord('Z'):
            x_list.append(chr(ord(x[i])-26+5))
        else:
            x_list.append(x[i])
    return ''.join(x_list)

def decoder(x):
    x_list = []
    for i in range(len(x)):
        if ord('f') <= ord(x[i]) <= ord('z') or ord('F') <= ord(x[i]) <= ord('Z'):
            x_list.append(chr(ord(x[i])-5))
        elif ord('a') <= ord(x[i]) <= ord('e') or ord('A') <= ord(x[i]) <= ord('E'):
            x_list.append(chr(ord(x[i])+26-5))
        else:
            x_list.append(x[i])
    return ''.join(x_list)

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

 

 task7

"""
Created on Mon May  9 20:57:16 2022

@author: ASUS
"""

def collatz(n):
    if n % 2 == 0:
        return n/2
    elif n % 2 != 0:
        return 3*n + 1

n = eval(input('Enter a positive integer:'))
try:
    if type(n) != int:
        raise
except:
    print('Error:must be a positive integer')
else:
    list = [n]
    while True:
        n = int(collatz(n))
        list.append(n)
        if n == 1:
            break
    print(list)

 

posted on 2022-05-09 19:51  QiQi-sakura  阅读(9)  评论(2编辑  收藏  举报