task1
print(sum) sum=42 print(sum) def inc(n): sum=n+1 print(sum) return sum sum=inc(7)+inc(7) print(sum)
1.打印sum函数
2.打印变量名为“sum”的变量
3.局部变量
4.全局变量
task2.2
list1=[1,9,8,4] print(sorted(list1)) print(sorted(list1,reverse=True)) print(sorted(list1,True))
是的
task2.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))
task3
def solve(a,b,c): """ 求解一元二次方程,返回方程的两个根 :param a,b,c: float 方程系数 :return: tuple """ delta=b*b-4*a*c delta_sprt=abs(delta)**0.5 p1=-b/2/a p2=delta_sprt/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
def list_generator(start,end,step=1): list=[start] i=start while i != end: i+=step list.append(i) 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)
task5
def is_prime(n): if n > 1: for i in range(2, int((n ** 0.5) + 1)): if n % i == 0: return False else: return True else: return False num=4 while num<=20: for i in range(2,num): if is_prime(i): if is_prime(num-i): print(f'{num}={i}+{num-i}') break num+=2
task6
def encoder(text): text=list(text) i=0 while i< len(text): if text[i].isalpha(): if text[i].islower(): if ord(text[i])<=117: text[i]=chr(ord(text[i])+5) else: text[i]=chr(ord(text[i])+5-26) if text[i].isupper(): if ord(text[i])<=85: text[i] = chr(ord(text[i]) + 5) else: text[i] = chr(ord(text[i]) + 5 - 26) i+=1 return ''.join(text) def decoder(text): text = list(text) i = 0 while i < len(text): if text[i].isalpha(): if text[i].islower(): if ord(text[i]) >= 102: text[i] = chr(ord(text[i]) - 5) else: text[i] = chr(ord(text[i]) - 5 + 26) if text[i].isupper(): if ord(text[i]) >= 46: text[i] = chr(ord(text[i]) - 5) else: text[i] = chr(ord(text[i]) - 5 + 26) i += 1 return ''.join(text) for i in range(3): title='Remote Interpreter Reinitialized' print(title.center(60,'*')) text=input('输入英文文本:') encoded_text=encoder(text) decoded_text=decoder(encoded_text) print('编码后的文本:',encoded_text) print('对编码后的文本解码:',decoded_text)
task7
def collatz(number): if number % 2 == 1: return number * 3+1 elif number % 2 ==0: return number // 2 for i in range(5): try: n = eval(input('Enter a positive integer:')) if type(n) is float or type(n) is str or n <= 0: raise ValueError('Error: must be a positive integer') except ValueError as e: print(repr(e)) print() except: print('Error: must be a positive integer') else: num=collatz(n) list=[n,num] while True: if num != 1: num=collatz(num) list.append(num) elif num == 1: break print(list)
task8
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,list,实验,print,def From: https://www.cnblogs.com/gkyi1/p/17412508.html