day22

day22
1.使用必填参数、默认参数、可变元组参数、可变字典参数(value)计算一下单词的长度之和。
def str_len(a,b="flask",*args,**kwargs):
result = 0
len1 = len(a)
len2 = len(b)
result += len1
result += len2
for i in args:
result += len(i)
for k in kwargs.values():
result += len(k)
print(result)
str("abc","abc","abc",name="abc") # 此时b已经被修改了,所以是12
str("abc",name="abc") # 此时b为默认的flask
2.使用map把[1,2,3]变为[2,3,4]
list(map(lambda x: x+1, [1,2,3]))
 
3.使用map,大写变小写
list(map(lambda x: chr(ord(x)+32),"ABC"))
 
4.打印2000-3000之间被7整除但不被5整除的数,以,(逗号)分隔
for i in range(2000,3000):
if i % 7 == 0 and i % 5 !=0:
print(i,end=",")
 
5.输出9*9口诀表
for i in range(1,10):
for k in range(1,i+1):
print("{}*{}={}".format(i,k,i*k), end=' ')
print()
 
6.计算1 - 1/2 + 1/3 - 1/4 + … + 1/99 - 1/100 + …直到最后一项的绝对值小于10的-5次幂为止
result = 0.0
n = 1
while True:
    if abs(1/n) < pow(10,-5):
        break
    else:
        if n%2 == 1:
            result += 1/n
            n += 1
        else:
            result -= 1/n
            n += 1
print(result)
 
7、编程将类似“China”这样的明文译成密文,
密码规律是:用字母表中原来
的字母后面第4个字母代替原来的字母,不能改变其大小写,如果超出了字母
表最后一个字母则回退到字母表中第一个字母
#a b c d e
#v w x y z
 
def encode_password(s):
    result = ""
    if not isinstance(s,str):
        print("请传入字符串")
    for c in s:
        if (c >= "a" and c <= "v") or (c >= "A" and c <= "Z"):
            result += chr(ord(c) + 4)
        else:
            result += chr(ord(c) - 22)
    return result
print(encode_password("China"))

posted @ 2019-12-26 08:15  绝世老中医  阅读(198)  评论(0编辑  收藏  举报