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)
task2_1.py
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))
task2_2.py
list1=[1,0,8,4] print(sorted(list1)) print(sorted(list1,reverse=True)) print(list1,True)
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): delta=b**2-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 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(x,y,s=1): l = [] while x<=y: l.append(x) x = x+s return l 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): i=1 sum=0 if n>1: while i<=n: if n%i==0: sum+=1 i+=1 else: sum+=0 i+=1 if sum<=2: return True else:return False s=[] for i in range(2,20): if is_prime(i)==True: s.append(i) print(s) for i in range(2,21,2): if i%2==0: for j in s: if (is_prime(j)and is_prime(i-j) and(j<=(i-j))): print(f'{i}={j}+{i-j}')
task6.py
def encoder(n1): s1 = [] s2 = [] for i in range(97, 123): s1.append(chr(i)) for i in range(65, 91): s2.append(chr(i)) n=list(n1) i=0 while i<len(n): if n[i] in s1: if ord(n[i])<=117: n[i]=chr(ord(n[i])+5) else:n[i]=chr(ord(n[i])+5-26) if n[i] in s2: if ord(n[i])<=85: n[i]=chr(ord(n[i])+5) else:n[i]=chr(ord(n[i])+5-26) i+=1 return ''.join(n) def decoder(n2): s1 = [] s2 = [] for i in range(97, 123): s1.append(chr(i)) for i in range(65, 91): s2.append(chr(i)) n = list(n2) i = 0 while i < len(n): if n[i] in s1: if ord(n[i])>=102: n[i] = chr(ord(n[i])-5) else: n[i] = chr(ord(n[i])-5+26) if n[i] in s2: if ord(n[i])>=46: n[i] = chr(ord(n[i])-5) else: n[i] = chr(ord(n[i])-5+26) i += 1 return ''.join(n) for i in range(2): title='Remote Interpreter Reinitialized' print(title.center(60,'*')) text=input('输入英文文本:') encoded_text=encoder(text) print(f'编码后的文本:{encoded_text}') decoded_text=decoder(encoded_text) print(f'对编码后文本解码:{decoded_text}')
task7.py
def collatz(n): if n%2==0: return int(n/2) elif n%2!=0: return n*3+1 for i in range(5): title='Remote Interpreter Reinitialized' print(title.center(60,'*')) num=input('Enter a positive integer:') if num.isalpha()==True: print('Error:must be a positive interger') if num.isdigit()==True: num=eval(num) if type(num)!=int or num<=0: print('Error:must be a positive interger') else: ans = [] ans.append(num) while num!=1: num=collatz(num) ans.append(num) print(ans)
task8.py
def func(n): def func1(n): if n==0: return 1 return 2*func1(n-1) return func1(n)-1 while True: x = input() if x == '#': print('计算结束') break n = int(x) ans = func(n) print(f'n = {n}, ans = {ans}')
标签:return,text,sum,py,实验,print,def From: https://www.cnblogs.com/M-H-Bi/p/17408181.html