实验四

#task1

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:全局变量

#task2—1 (代码框架)
def func1(a, b, c, d, e, f):
    '''
    返回参数a,b,c,d,e,f构成的列表
    默认,参数按位置传递; 也支持关键字传递
    '''
    return [a,b,c,d,e,f]


def func2(a, b, c,*, d, e, f):
    '''
    返回参数a,b,c,d,e,f构成的列表
    *后面的参数只能按关键字传递
    '''
    return [a,b,c,d,e,f]


def func3(a, b, c, /, d, e, f):
    '''
    返回参数a,b,c,d,e,f构成的列表
    /前面的参数只能按位置传递
    '''
    return [a,b,c,d,e,f]


# func1调用:按位置传递、按参数传递都可以
print( func1(1,9,2,0,5,3) )
print( func1(a=1, b=9, c=2, d=0, e=5, f=3) )
print( func1(1,9,2, f=3, d=0, e=5))


# func2调用:d,e,f必须按关键字传递
print( func2(11, 99, 22, d=0, e=55, f=33) )
print( func2(a=11, b=99, c=22, d=0, e=55, f=33) )


# func3调用:a,b,c必须按位置传递
print( func3(111, 999, 222, 0, 555, 333))
print( func3(111, 999, 222, d=0, e=555, f=333) )

#在line33后插入
 print( func2(11, 99, 22, 0, 55, 33) )

 func2函数要键入3个参数,而不是6个。d,e,f 需要用关键字参数传入

#在line38后插入
 print(func3(a=111, b=999, c=222, 0, 555, 333) )

 前面的参数只能按位置传递

#task2-2
list1 = [1, 9, 8, 4]
print( sorted(list1) )
print( sorted(list1, reverse=True) )
print( sorted(list1, True) )

 reversed必须要关键字参数传入

#task2-3
def func(a, b, c, /, *, d, e, f):
    return( [a,b,c,d,e,f] )
# 补足一行代码,调用func()并打印生成的列表,使得结果为[1,2,3,4,5,6]
# 待补足。。。
print(func(1, 2, 3, d=4, e=5, f=6))

#task3
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


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

#line22前插入
 print(solve.__doc__)

解释语句被打印出来

#task4
def  list_generator(x,y,step=1):
    output = []
    while x<= y:
        output.append(x)
        x += step
    return output


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
def is_prime(n):
    for i in range(2, int(n ** 0.5) + 1):
        if not n % i:
            return False
    else:
        return True


for i in range(2, 21, 2):
    for sim in range(2, i):
        if is_prime(i - sim):
            print(f"{i} = {sim} + {i - sim}")
            break

#task6

def encoder(text):
    sentence = []
    for i in text:
        if i.isupper():
            sentence.append(chr((ord(i)-60)%26+65))
        elif i.islower():
            sentence.append(chr((ord(i)-92)%26+97))
        else :
            sentence.append(i)
    return ''.join(sentence)


def decoder(text):
    sentence = []
    for i in text:
        if i.isupper():
            sentence.append(chr((ord(i)-44)%26+65))
        elif i.islower():
            sentence.append(chr((ord(i)-76)%26+97))
        else :
            sentence.append(i)
    return ''.join(sentence)

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

#task7
def collatz(n):
    num = [n]
    while num[-1] != 1:
        if not num[-1] % 2:
            num.append(int(num[-1]/2))
        else:
            num.append(int(3*num[-1]+1))
    return num


try:
    n = eval(input("输入正整数:"))
    if n <= 0 or int(n) != n:
        raise
except:
    print("输入错误, 不是一个有效的整数...")
else:
    print(collatz(n))

 

posted @ 2022-05-07 13:10  云处安  阅读(19)  评论(3编辑  收藏  举报