第四次实验

print(sum)
sum=42
print(sum)

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

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

不是

1:内置 2:全局  3:局部  4:全局

 

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

没有

 

def list_generator(begin,end,model=1):
    a=[]
    while begin<=end:
        a.append(begin)
        begin+=model
    return a

        
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(n):
    if n==1:
        return False
    elif n==2:
        return True
    else:
        a=2
        for i in range(2,n):
            if n%i==0:
                return False
            else:
                a+=1
            if a==n:
                return True

    
for j in range(2,21,2):
    for x in range(1,21):
        if is_prime(x)==True and is_prime(j-x)==True and x<=j-x:
            print(f'{j}={x}+{j-x}')
            break

 

 

def encoder(x):
    ls=[]
    x=list(x)
    for i in x:
        if 97<=ord(i.lower())<=117:
            i=chr(ord(i)+5)
        elif 118<=ord(i.lower())<=122:
            i=chr(ord(i)-21)
        ls.append(i)
    return ''.join(ls)

def decoder(x):
    ls=[]
    x=list(x)
    for j in x:
        if 102<=ord(j.lower())<=122:
            j=chr(ord(j)-5)
        elif 97<=ord(j.lower())<=101:
            j=chr(ord(j)+21)
        ls.append(j)
    return ''.join(ls)


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


            
    

def collatz(n):
    if n%2:
        n=3*n+1
    else:
        n=int(n/2)
    return n

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

 

posted @ 2022-05-10 13:03  张杰的  阅读(6)  评论(3编辑  收藏  举报