第一周代码整理

字典练习题

1 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
l = [11,22,33,44,55,66,77,88,99,90]
d = {'k1':[],'k2':[]}
for i in l:
    if i >= 66:
        d['k1'].append(i)
    else:
        d['k2'].append(i)
print(d)
View Code
2 统计s='hello alex alex say hello sb sb'中每个单词的个数
结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
s='hello alex alex say hello sb sb'
l = s.split() #1 切成列表遍历单词 count统计重复次数
d = {}
for i in l:
    d.setdefault(i,l.count(i)) #2 字典的setdefault功能:没有key就加 有key就不变
print(d)
View Code

 

集合练习题

  一.关系运算
  有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
  pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
  linuxs={'wupeiqi','oldboy','gangdan'}
  1. 求出即报名python又报名linux课程的学员名字集合
  2. 求出所有报名的学生名字集合
  3. 求出只报名python课程的学员名字
  4. 求出没有同时这两门课程的学员名字集合
pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
linuxs={'wupeiqi','oldboy','gangdan'}
print(pythons & linuxs)
print(pythons | linuxs)
print(pythons - linuxs)
print(pythons ^ linuxs)
View Code
   二.去重
   1. 有列表l=['a.txt','b',1,'a.txt','a.txt'],列表元素均为可hash类型,去重,得到新列表,且新列表无需保持列表原来的顺序
   2.在上题的基础上,保存列表原来的顺序
   3.去除文件中重复的行,肯定要保持文件内容的顺序不变
   4.有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
l = [
{'name': 'egon', 'age': 18, 'sex': 'male'},
{'name': 'alex', 'age': 73, 'sex': 'male'},
{'name': 'egon', 'age': 20, 'sex': 'female'},
{'name': 'egon', 'age': 18, 'sex': 'male'},
{'name': 'egon', 'age': 18, 'sex': 'male'},
]
l=['a.txt','b',1,'a.txt','a.txt']
new_l = list(set(l))
print(new_l)

l=['a.txt','b',1,'a.txt','a.txt']
new_l = []
for i in l:
    if i not in new_l:
        new_l.append(i)
print(new_l)

import os
with open('db.txt','r',encoding='utf-8') as read_f,\
        open('.db.txt.txt.swap','w',encoding='utf-8') as write_f:
    s=set()
    for line in read_f:
        if line not in s:
            s.add(line)
            write_f.write(line)
os.remove('db.txt.txt')
os.rename('.db.txt.txt.swap','db.txt.txt')

l = [
    {'name': 'egon', 'age': 18, 'sex': 'male'},
    {'name': 'alex', 'age': 73, 'sex': 'male'},
    {'name': 'egon', 'age': 20, 'sex': 'female'},
    {'name': 'egon', 'age': 18, 'sex': 'male'},
    {'name': 'egon', 'age': 18, 'sex': 'male'},
]
new_l = []
for i in l:
    if i not in new_l:
        new_l.append(i)
print(new_l)
View Code

 

登陆接口

基础需求:
让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序
name_db = 'xjj'
pwd_db = '123'
count = 1
while count <= 3:
    name_inp = input('username>>: ').strip()
    pwd_inp = input('password>>: ')
    if name_db == name_inp and pwd_db == pwd_inp:
        print('login success')
        break
    else:
        print('login failure')
        count += 1
View Code

升级需求:
可以支持多个用户登录 (提示,通过列表存多个账户信息)
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
#db.txt内容:egon1|egon2|
dic={
    'egon1':{'password':'123','count':0},
    'egon2':{'password':'123','count':0},
    'egon3':{'password':'123','count':0},
}

count=0
while True:
    name=input('u>>: ')
    if name not in dic:
        print('用户不存在')
        continue

    with open('a.txt','r') as f:
        lock_users=f.read().split('|')
        if name  in lock_users:
            print('用户%s已经被锁定' %name)
            break

    if dic[name]['count'] > 2:
        print('尝试次数过多,锁定')
        with open('db.txt','a.txt') as f:
            f.write('%s|' %name)
        break

    password=input('p>>: ')

    if password == dic[name]['password']:
        print('登录成功')
        break
    else:
        print('用户名或密码错误')
        dic[name]['count']+=1
View Code

简单购物车

简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,
如果输入为空或其他非法输入则要求用户重新输入 
msg_dic = {
'apple': 10,
'tesla': 100000,
'mac': 3000,
'lenovo': 30000,
'chicken': 10,
}
shoppint_cart = []
while True:
    for k,v in msg_dic.items():
        info = '商品名:{} 价格:{}'.format(k,v)
        print(info.center(30,' '))
    name = input('请输入商品名').strip()
    if name not in msg_dic:
        print('商品名不对,从新输入')
        continue
    while True:
        count = input('请输入购买个数').strip()
        if not count.isdigit():
            print('购买个数必须为整数')
        else:
            count = int(count)
            break
    for item in shoppint_cart:
        if item['name'] == name:
            item['count'] += count
            break
    else:
        price = msg_dic[name]
        info = {'name':name,'count':count,'price':price}
        shoppint_cart.append(info)
    print(shoppint_cart)
View Code

 

posted @ 2018-06-03 22:26  xujinjin  阅读(185)  评论(0编辑  收藏  举报