實驗四 函式與異常處理應用程式設計
阿新 • • 發佈:2022-05-08
------------恢復內容開始------------
------------恢復內容開始------------
------------恢復內容開始------------
#test1.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 42
line3 and line7 n+1
line11 7+1+7+1=16
#test2.pydef 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] # 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) )
line33:
line38:
#test2_2.py list1 = [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
否
#test2_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))
#test_3.py 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 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()
#test_4.py def list_generator(a,b,c=1): y = [] while a <= b: y.append(a) a+=c return y list1 = list_generator(-5, 5) print(list1) list2 = list_generator(-5, 5, 2) print(list2) list3 = list_generator(1, 5, 0.5) print(list3)
#test.5py 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
#test_6.py def encoder(x): a=5 b='' for i in x: if 65<=ord(i)<=85: b+=chr(ord(i)+5) elif 97<=ord(i)<=117: b+=chr(ord(i)+5) elif 85<ord(i)<=90: b+=chr(65+(a-(90-ord(i)))) elif 117<ord(i)<=122: b+=chr(97+(a-(122-ord(i)))) else: b+=i return b def decoder(y): c=5 d='' for j in y: if 70<=ord(j)<=90: d+=chr(ord(j)-5) elif 102<=ord(j)<=122: d+=chr(ord(j)-5) elif 65<=ord(j)<70: d+=chr(90-(c-(ord(j)-65))) elif 97<=ord(j)<102: d+=chr(122-(c-(ord(j)-97))) else: d+=j return d x=input('輸入英語文字:') print('編碼後的文字:',encoder(x)) print('對編碼後的文字解碼:',decoder(encoder(x)))
#test_7.py def collatz(n): if n%2==0: n=n/2 else: n=3*n+1 return int(n) try: n=eval(input('Enter a positive integer:')) if n<=0 or type(n)!=int: raise except: print('Error: must be a positive integer') else: list=[] while n!=1: list.append(n) n=collatz(n) list.append(n) print(list)
------------恢復內容結束------------
------------恢復內容結束------------
------------恢復內容結束------------