print(sum)
sum = 42
print(sum)
def inc(n):
sum = n+1
print(sum)
return sum
sum = inc(7) + inc(7)
print(sum)

1.Built-in

2.Global

3.Local

4.Global

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):
'''
求解一元二次方程, 返回方程的两个根
: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()

def list_generator(begin, end, step=1):
ls = []
a = begin
b = end
while a <= b:
ls.append(a)
a += step
return ls

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(a):
for i in range(2, int(a)+1):
if not n%i:
return False
else:
return True

ls = []
for i in range(20):
if is_prime(i):
ls.append(i)

def summary(x):
i = 0
while True:
for j in range(i, len(ls)):
s = ls[i]+ls[j]
if x == s:
s = f"{s} = {ls[i]} + {ls[j]}"
return s
i += 1

for i in range(4, 21, 2):
print(summary(i))

def encoder(a):
"""对文本加密"""
result = ''
for i in range(len(a)):
if ord(a[i]) in range(65, 91):
result+=chr((ord(a[i])-65+5)%26+65)
elif ord(a[i]) in range(97, 123):
result+=chr((ord(a[i])-97+5)%26+97)
else:
result += a[i]
return result
def decoder(a):
"""解码加密文本"""
result = ''
for i in range(len(a)):
if ord(a[i]) in range(65, 91):
result+=chr((ord(a[i])-65+26-5)%26+65)
elif ord(a[i]) in range(97, 123):
result+=chr((ord(a[i])-97+26-5)%26+97)
else:
result += a[i]
return result
text = input("输入英文文本： ")
print(f"编码后的文本： {encoder(text)}")
print(f"对编码后的文本解码： {decoder(encoder(text))}") 

ef collatz(a):
"""验证Collatz猜想"""
if not a % 2:
return a / 2
else:
return 3 * a + 1

try:
n = int(input("Enter a positive integer: "))
if n <= 0:
raise ValueError
ls = [n]
while n != 1:
n = collatz(n)
ls.append(n)
print([int('{:.0f}'.format(i)) for i in ls])
except ValueError:
print("Error:must be a positive integer")

posted on 2022-05-10 14:46  JskyR  阅读(15)  评论(1编辑  收藏  举报