实验4:函数与异常处理应用编程
作者:互联网
任务实验1:
1 print(sum) 2 sum=42 3 print(sum) 4 def inc(n): 5 sum=n+1 6 print(sum) 7 return sum 8 sum=inc(7) +inc(7) 9 print(sum)
问题答案:代码中的sum并不是同一个变量名,line1的sum是内置作用域,line3的sum是全局作用域,line7的sum是局部作用域,line9的sum是全局作用域。
任务实验2.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] 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) )
任务实验2.2:
list1= [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
问题答案:
python内置函数sorted()中,参数reverse的传递方式必须使用关键字传递
任务实验2.3:
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))
任务实验3:
1 def solve(a, b, c): 2 '''求解一元二次方程, 返回方程的两个根 3 :para: a,b,c: int 方程系数 4 :return: tuple 5 ''' 6 delta=b*b-4*a*c 7 delta_sqrt=abs(delta)**0.5 8 p1=-b/2/a; 9 p2=delta_sqrt/2/a 10 11 if delta>=0: 12 root1=p1+p2 13 root2=p1-p2 14 else: 15 root1=complex(p1, p2) 16 root2=complex(p1, -p2) 17 return root1, root2 18 19 while True: 20 try: 21 a,b,c=eval(input('Enter eqution coefficient: ')) 22 if a==0: 23 raise 24 except: 25 print('invalid input, or, a is zero') 26 break 27 else: 28 root1, root2=solve(a, b, c) 29 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 30 print()
任务实验4:
def list_generator (begin,end,step=1): list=[] while begin<=end: list.append(begin) begin=begin+step return list list1=list_generator(-5, 5) print(list1) list2=list_generator(-5, 5, 2) print(list2) list3=list_generator(1, 5, 0.5) print(list3)
任务实验5:
def isPrime(x): i=2 while i<=x-1 and x%i!=0: i=i+1 if i==x: return True else: return False for j in range(4,21,2): for n in range(2,j): if isPrime(n) and isPrime(j-n): print(f'{j}={n}+{j-n}') break
任务实验6:
1 def encoder(x): 2 a=5 3 b='' 4 for i in x: 5 if 65<=ord(i)<=85: 6 b+=chr(ord(i)+5) 7 elif 97<=ord(i)<=117: 8 b+=chr(ord(i)+5) 9 elif 85<ord(i)<=90: 10 b+=chr(65+(a-(90-ord(i)))) 11 elif 117<ord(i)<=122: 12 b+=chr(97+(a-(122-ord(i)))) 13 else: 14 b+=i 15 return b 16 def decoder(y): 17 c=5 18 d='' 19 for j in y: 20 if 70<=ord(j)<=90: 21 d+=chr(ord(j)-5) 22 elif 102<=ord(j)<=122: 23 d+=chr(ord(j)-5) 24 elif 65<=ord(j)<70: 25 d+=chr(90-(c-(ord(j)-65))) 26 elif 97<=ord(j)<102: 27 d+=chr(122-(c-(ord(j)-97))) 28 else: 29 d+=j 30 return d 31 x=input('输入英语文本:') 32 print('编码后的文本:',encoder(x)) 33 print('对编码后的文本解码:',decoder(encoder(x)))
任务实验7:
1 def collatz(n): 2 x=[n] 3 while n!=1: 4 if n%2==0: 5 n=n/2 6 else: 7 n=3*n+1 8 x.append(int(n)) 9 return x 10 try: 11 n=int(input('Enter a positive integer:')) 12 if n<=0: 13 raise 14 except: 15 print('Error: must be a positive integer') 16 else: 17 print(collatz(n))
标签:return,函数,sum,编程,实验,print,root1,def,root2 来源: https://www.cnblogs.com/day-day/p/16221820.html