首页 > 其他分享 >实验4

实验4

时间:2023-05-23 21:45:30浏览次数:35  
标签:return sum py 实验 print True def

实验任务1

task1.py

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,line11的sum是全局作用域,line7的sum是局部作用域。

实验任务2

task2_2.py

答:内置函数sorted()中参数reverse一定要用关键字传递。

task2_3.py

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.py

def solve(a, b, c):
    '''
    求解一元二次方程, 返回方程的两个根
    :para: a,b,c: float 方程系数
    :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:
        t = input('输入一元二次方程系数a b c, 或者,输入#结束: ')
        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.py

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

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.py

def is_prime(n):
    if n == 2:
        return True
    elif n != 1:
        for i in range(2,n):
            if n == 2:
                return True
            elif n % i == 0:
                break
            else:
                return True

for i in range(2,20+1,2):
    flag = True
    for j in range(2, i):
        if is_prime(j) == True and is_prime(i - j) == True and flag == True:
            print(f'{i}={j}+{i-j}')
            flag = False

 task6.py

def encoder(text):
    new_text1 = ''
    for i in text:
        if 'a' <= i <='z':
            i = chr((ord(i)-97+5)%26 + 97)
            new_text1 += i
        elif 'A' <= i <= 'Z':
            i = chr((ord(i)-65+5)%26 + 65)
            new_text1 += i
        else:new_text1 += i
    return new_text1

def decoder(text):
    new_text2 = ''
    for i in text:
        if 'a' <= i <='z':
            i = chr((ord(i)-97-5)%26 + 97)
            new_text2 += i
        elif 'A' <= i <= 'Z':
            i = chr((ord(i)-65-5)%26 + 65)
            new_text2 += i
        else:new_text2 += i
    return new_text2

 task7.py

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

 task8.py

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

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

 

标签:return,sum,py,实验,print,True,def
From: https://www.cnblogs.com/yunsir/p/17408150.html

相关文章

  • 实验4 函数与异常处理编程
    task1print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)答案:不是。line1为内置作用域,line3和line11为全局作用域,line7为局部作用域。task2-1deffunc1(a,b,c,d,e,f):'''返回参数......
  • 实验4 函数与异常处理编程
    task1print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)运行结果: task2.1deffunc1(a,b,c,d,e,f):return[a,b,c,d,e,f]deffunc2(a,b,c,*,d,e,f):#返回参数a,b,c,d,e,f构成......
  • 实验四
    print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)答:不是。line1的作用域为全局作用域·,line3的作用域是外部嵌套作用域,line7的作用域为局部作用域,line11的作用域为局部作用域。task2-1deffunc1(a,b,c......
  • 实验4
    task1print(sum)sum=42print(sum)definc(n):sum=n+1print(sum)returnsumsum=inc(7)+inc(7)print(sum)task2deffunc1(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):......
  • 实验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,......