首页 > 其他分享 >实验4

实验4

时间:2023-05-23 20:25:01浏览次数:31  
标签:return sum list re 实验 print def

task1

print(sum)
sum=42
print(sum)

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

task2

def func1(a,b,c,d,e,f):
    return [a,b,c,d,e,f]
def func2(a,b,c,*,d,e,f):
    return [a,b,c,d,e,f]
def func3(a,b,c,/,d,e,f):
    return [a,b,c,d,e,f]
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))
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))
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])
s=func(1,2,3,d=4,e=5,f=6)
print(s)

task3

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:
        t=input('输入一元二次方程系数abc,或者输入#结束:')
        if t=='#':
            print('结束计算,退出')
            break
        a,b,c=map(float,t.split())
        if a==0:
            raise ValueError('a=0,不是一元二次方程')
    except ValueError as e:
        print(repr(e))
        print()
    except:
        print('有其它错误发生\n')
    else:
        root1,root2=solve(a,b,c)
        print(f'root1={root1:.2f},root2={root2:.2f}')
        print()

task4

def list_generator(a,b,c=1):
    i=a
    s=[i]
    while i<b:
        i+=c
        s.append(i)
    return s

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(s):
    sum=0
    for i in range(2,s):
        if s%i==0:
            sum+=i
    ans=sum==0
    return ans

for i in range(2,21,2):
    s=[]
    for j in range(2,i):
        if is_prime(j)==True:
            s.append(j)
    for m in s:
        if (i-m)in s:
            print(f'{i}={m}+{i-m}')
            break

task6

list_s=['a','b','c','d','e','f','g','h','i','j','k',
        'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
list_b=['A','B','C','D','E','F','G','H','I','J','K',
        'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
def encoder(x):
    re=[]
    for i in str(x):
        if i in list_s:
            a=list_s.index(i)
            if a<=20:
                i=list_s[a+5]
                re.append(i)
            else:
                i=list_s[a+5-26]
                re.append(i)
        elif i in list_b:
            a=list_b.index(i)
            if a<=20:
                i=list_b[a+5]
                re.append(i)
            else:
                i=list_b[a+5-26]
                re.append(i)
        else:
            re.append(i)
    re=''.join(re)
    return re
def decoder(x):
    re=[]
    for i in str(x):
        if i in list_s:
            a=list_s.index(i)
            if a>=5:
                i=list_s[a-5]
                re.append(i)
            else:
                i=list_s[a+26-5]
                re.append(i)
        elif i in list_b:
            a=list_b.index(i)
            if a>=5:
                i=list_b[a-5]
                re.append(i)
            else:
                i=list_b[a+26-5]
                re.append(i)
        else:
            re.append(i)
    re=''.join(re)
    return re

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

task7

def collatz(n):
    re=[n]
    while True:
        if n==1:
            break
        if n%2==0:
            n=int(n/2)
            re.append(n)
        else:
            n=3*n+1
            re.append(n)
    return re
n=input('Enter a positive intefer:')
try:
    n=int(n)
    if n<=0 :
        raise
except:
    print('Error: must be a positive integer')
else:
    print(collatz(n))

task8

def func(n):
    if n==0:
        return 0
    else:
        return 2*(func(n-1)+1)-1

while True:
    x=input()
    if x=='#':
        print('计算结束')
        break
    n=int(x)
    ans=func(n)
    print(f'n={n},ans={ans}')

 

标签:return,sum,list,re,实验,print,def
From: https://www.cnblogs.com/heqifan666/p/17426257.html

相关文章

  • 实验4 函数与异常处理编程
    实验结论实验任务1程序源代码print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)运行测试截图问题回答:不是同一个对象的名称。line1是内置作用域,line3和line11是全局作用域,而line7是在inc中的局......
  • 实验四
    task1源代码:print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)问题:task1.py源码中,共有4处有python语句 print(sum) (line1,line3,line7,line11)。 这4处使用的标识符sum是同一个对象的......
  • 实验4函数与异常处理程序
    1.实验任务1print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)问题:不是。line1中的sum是指Python的内置函数;line3中的sum指的是line2中的全局变量sum;line7中的sum指的是inc函数中的局部变......
  • 实验四
    任务1print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)任务22.1deffunc1(a,b,c,d,e,f):return[a,b,c,d,e,f]deffunc2(a,b,c,*,d,e,f):return[a,b,c,d,e,f]deffunc3(a,b,c,/,d,e,f)......
  • 实验四 函数与异常处理编程
    print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)1空白2第二行的sum3函数里的sum4第十行的sum任务2deffunc1(a,b,c,d,e,f):return[a,b,c,d,e,f]deffunc2(a,b,c,*,d,e,f):return[......
  • 实验4
    Task1print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)ViewCode不是。line1:全局作用域line3:全局作用域line7:局部作用域line11:全局作用域task2.1deffunc1(a,b,c,d,e,f):return[a,b,c,d,......
  • 实验4
    task1实验源码:1print(sum)2sum=423print(sum)45definc(n):6sum=n+17print(sum)8returnsum910sum=inc(7)+inc(7)11print(sum)运行截图: 答:不是。line1:全局作用域line3:全局作用域line7:局部作用域line11:全局作用域......
  • 实验四
    试验任务一:1print(sum)2sum=423print(sum)4definc(n):5sum=n+16print(sum)7returnsum8sum=inc(7)+inc(7)9print(sum) 答:不一样,line1的sum没有定义,line3的sum作用域为全局,line7的sum的作用域为函数inc内部,line11的sum作用域为全局......
  • 实验4 函数与异常处理编程
    task1程序源码:1print(sum)2sum=423print(sum)45definc(n):6sum=n+17print(sum)8returnsum9sum=inc(7)+inc(7)10print(sum)运行截图:Q1:  task1.py源码中,共有4处有python语句print(sum)(line1,line3,line7,line11......
  • 实验四
    任务一实验代码1print(sum)2sum=423print(sum)45definc(n):6sum=n+17print(sum)8returnsum91011sum=inc(7)+inc(7)12print(sum)task运行结果截图不是同一个对象名称 1.Built-in。2和3Global。4.Local任务二......