# 实验四

 1 print(sum)
2 sum = 42
3 print(sum)
4
5 def inc(n):
6     sum=n+1
7     print(sum)
8     return sum
9
10 sum=inc(7)+inc(7)
11 print(sum)

list1=[1,9,8,4]
print(sorted(list1))
print(sorted(list1, reverse=True))
print(sorted(list1, True))

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))

def solve(a,b,c):
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()

def list_generator(x,y,z=1):
a=[]
while x<=y:
a.append(x)
x=x+z
return a
list1=list_generator(-5,5)
print(list1)
list2=list_generator(-5,5,2)
print(list2)
list3=list_generator(1,5,0.5)
print(list3)

def is_prime(x):
for i in range(2,x):
if x%i==0:
return False
return True
for i in range(4,21,2):
for t in range(2,i):
if is_prime(t)==True and is_prime(i-t)==True:
print(f'{i}={t}+{i-t}')
break

def encoder(x):
y=list(x)
for j in range(len(y)):
x=y[j]
a=[chr(i) for i in range(97,123)]
b=[chr(i) for i in range(65,91)]
if x in a:
t=a.index(x)+5
if t>25:
t=t-26
y[j]=a[t]
elif x in b:
s = b.index(x) + 5
if s>25:
s=s-26
y[j]=b[s]
else:
y[j]=x
return ''.join(y)
def decoder(x):
y=list(x)
for j in range(len(y)):
x=y[j]
a=[chr(i) for i in range(97,123)]
b=[chr(i) for i in range(65,91)]
if x in a:
t=a.index(x)-5
if t<0:
t=t+26
y[j]=a[t]
elif x in b:
s = b.index(x)-5
if s<0:
s=s+26
y[j]=b[s]
else:
y[j]=x
return ''.join(y)
t=input('输入英文文本:')
a=encoder(t)
b=decoder(a)
print(f'编码后的文本:{a}')
print(f'对编码后的文本解码:{b}')

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

posted @ 2022-05-10 20:31  雷建彬  阅读(2)  评论(1编辑  收藏  举报