实验4

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

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]
# python允许函数形参中出现/的版本是3.8之后
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) )

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

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

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

import numpy as np
def list_generator(a,b,c=1):
    return list(np.arange(a,b,c))
    '''
    生成指定范围区间、指定步长的列表
    :param a:区间左界
    :param b:区间右界
    :param c:不常,默认为1
    :return:返回生成的列表
    '''
list1 = list_generator(-5, 5)
print(list1)
list2 = list_generator(-5, 5, 2)
print(list2)
list3 = list_generator(1, 5, 0.5)
print(list3)

def is_prime(x):
    for i in range(2, int(x ** 0.5) + 1):
        if x % i == 0:
            return False
    return True
def caixiang(x):
    for i in range(2,x):
        if is_prime(i) and is_prime(x-i):
            print(f'{x} = {i} + {x-i}')
            break
caixiang(4)

import string
xiaoxie=list(string.ascii_lowercase)
daxie=list(string.ascii_uppercase)
def encoder(content):
    content=list(content)
    for i in range(len(content)):
        if content[i] in xiaoxie:
            content[i]=xiaoxie[(xiaoxie.index(content[i])+5)%26]
        elif content[i] in daxie:
            content[i] = daxie[(daxie.index(content[i]) + 5) % 26]
    return ''.join(content)
def decoder(content):
    content = list(content)
    for i in range(len(content)):
        if content[i] in xiaoxie:
            content[i] = xiaoxie[(xiaoxie.index(content[i]) + 26 -5) % 26]
        elif content[i] in daxie:
            content[i] = daxie[(daxie.index(content[i]) + 26 -5) % 26]
    return ''.join(content)
content='hi,May'
print('输入的英文文本:',content)
encoder_content=encoder(content)
print('编码后的文本:',encoder_content)
decoder_content=decoder(encoder_content)
print('对编码后的文本解码:',decoder_content)

def collatz():
    x = int(input('请输入一个正整数:'))
    results=[]
    if x<=0:
        print('Error: must be a positive integer')
        raise
    else:
        while x!=1:
            results.append(x)
            if x%2==0:
                x=x//2
            else:
                x=3*x+1
    results.append(1)
    print(results)
collatz()

 

posted @ 2022-05-09 20:32  严成娜  阅读(9)  评论(1编辑  收藏  举报