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

不是;line1、3、11是全局作用域。line7是函数作用域。
1 list1 = [1, 9, 8, 4]
2 print( sorted(list1) )
3 print( sorted(list1, reverse=True) )
4 print( sorted(list1, True) )

1 def func(a, b, c, /, *, d, e, f):
2     return( [a,b,c,d,e,f] )
3 print(func(1,2,3,d=4,e=5,f=6))

 1 def solve(a, b, c):
 2     '''
 3     求解一元二次方程, 返回方程的两个根
 4     :para: a,b,c: int 方程系数
 5     :return: tuple
 6     '''
 7     delta = b*b - 4*a*c
 8     delta_sqrt = abs(delta)**0.5
 9     p1 = -b/2/a;
10     p2 = delta_sqrt/2/a
11     if delta>=0:
12         root1 = p1 + p2
13         root2 = p1 - p2
14     else:
15         root1 = complex(p1, p2)
16         root2 = complex(p1, -p2)
17     return root1, root2
18 print(solve.__doc__)
19 while True:
20         try:
21             a,b,c = eval(input('Enter eqution coefficient: '))
22             if a == 0:
23                 raise
24         except:
25                 print('invalid input, or, a is zero')
26                 break
27         else:
28             root1, root2 = solve(a, b, c)
29             print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
30             print()

 1 def list_generator(a, b, c = 1):
 2     list = []
 3     n = a
 4     while(n <= 5):
 5         list.append(n)
 6         n = n + c
 7     return list
 8 list1 = list_generator(-5, 5)
 9 print(list1)
10 list2 = list_generator(-5, 5, 2)
11 print(list2)
12 list3 = list_generator(1, 5, 0.5)
13 print(list3)

 1 def is_prime(n):
 2     if n==1:
 3         return False
 4     elif n==2:
 5         return True
 6     else:
 7         for i in range(2,n):
 8             if n%i==0:
 9                 return False
10                 break
11             else:
12                 return True
13 sushu=[]
14 for i in range(1,21):
15     if is_prime(i)==True:
16         sushu.append(i)
17 oushu=[]
18 for i in range(1,21):
19     if i%2==0:
20         oushu.append(i)
21 for n in oushu:
22     for i in range(len(sushu)):
23         for j in range(len(sushu)):
24             if(n == sushu[i] + sushu[j]):
25                 print('{} = {} + {}'.format(n, sushu[i], sushu[j]))
26                 break
27         if(n == sushu[i] + sushu[j]):
28             break

 1 if __name__ == '__main__':
 2     main()
 3 import string
 4 s=input('输入英文文本:')
 5 low=string.ascii_lowercase+'abcde'
 6 upp=low.upper()
 7 l=low+upp
 8 def encoder(s):
 9     for i in s:
10         if i.isalpha():
11             s=s.replace(i,l[l.find(i)+5])
12     return s
13 s2=encoder(s)
14 l2=l[::-1]
15 def decoder(s2):
16     for i in s2:
17         if i.isalpha():
18             s2=s2.replace(i,l2[l2.find(i)+5])
19     return s2
20 print(f'编码后的文本:{encoder(s)}')
21 print(f'对编码后的文本解码:{decoder(s2)}')

 1 def collatz(x):
 2     if(x % 2 == 0):
 3         x = int(x / 2)
 4     else:
 5         x = 3 * x + 1
 6     return x
 7 
 8 list = []
 9 
10 try:
11     n = eval(input('Enter a positive integer: '))
12 
13     if(n <= 0):
14         raise
15     elif(int(n) != n):
16         raise
17 
18     while True:
19         if(n == 1):
20             list.append(n)
21             break
22         else:
23             list.append(int(n))
24             n = collatz(n)
25     print(list)
26 except:
27     print('Error: must be a positive integer')

 

posted on 2022-05-09 22:10  starry。  阅读(11)  评论(1编辑  收藏  举报