练习题



1.计算100-300之间所有能被3和7整除的所有数之和
sum=0
for i in range(100,301):
if i%3==0 and i%7==0:
sum=sum+i
#sum+=i
print(sum)

2.定义一个函数统计字符串中大写字母,小写字母,数字的个数,并返回结果

l=[]
def func(arg):
num=0
sum_num=0
low_num=0
upp_num=0
for i in arg:
#print(i)
if i.isdigit():
sum_num+=1
elif i.islower():
low_num+=1
elif i.isupper():
upp_num+=1
else:
num+=1

l.append(sum_num), l.append(low_num), l.append(upp_num), l.append(num)
return l

r=func('165AAA1dd dd')
print(r)

3.获取两个列表l1=[11,22,22],l2=[22,33,44]中相同的元素集合

l1=[11,22,33]
l2=[22,33,44]
l3=[]
for i in l1:
for j in l2:
if i==j:
l3.append(i)

print(l3)

4.将字符串老男人转化成utf-8编码的字节类型
str='老男人'
print(bytes(str,encoding="utf-8"))

5.利用内置函数zip(),实现功能
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
l1=['wo',11,22,33]
l2=['shi',11,22,33]
l3=['zhong',11,22,33]
l4=['guo',11,22,33]
l5=['ren',11,22,33]
请获取字符串s="wo shi zhong guo ren"

import zip

l1=['wo',11,22,33]
l2=['shi',11,22,33]
l3=['zhong',11,22,33]
l4=['guo',11,22,33]
l5=['ren',11,22,33]
s=list(zip(l1,l2,l3,l4,l5))[0]
print(" ".join(s))

6.注册登录并加密

import haslib

def func_md5(arg):
hash=hashlib.md5(bytes("chu"))
hash.update(arg)
return hash.hexdigest()

def register(user,pwd):

with open("user.db","ab") as f:
temp=user+"|"+func_md5(pwd)
f.write(temp+"\n")

def login(user,pwd):

with open("user.db","rb") as f:

for i in f.readlines():
#print(i,i.split(),i.split("|"))
a,b=i.strip().split("|")
if a==user and b==func_md5(pwd):
return 1
else:
return 0



def func():
temp=input("1、登录;2、注册")
if temp==1:
username = raw_input("username:")
password = raw_input("password:")
ret=login(username,password)
if ret ==1:
print("登录成功")
else:
print("用户名密码不匹配")
else:
username = raw_input("username:")
password = raw_input("password:")
register(username,password)


func()




posted @ 2018-08-09 09:19  旅行没有终点  阅读(223)  评论(0编辑  收藏  举报