python 基础应用5-简单购物车

1、列表去重

#列表去重
li = [1,2,33,33,2,1,4,5,6,6]
set1 = set(li)# 转为集合
li = list(set1)# 转为列表
print(li)#[1, 2, 33, 4, 5, 6]

2、删除所含输入字母或数字的键值对

#删除所含输入字母或数字的键值对
dic = {'k1':1,'k2':2,'j1':3,'j2':4}
l = []
print(dic)
s = input('输入所需删键值对中所含字母或数字:')
for i in dic:
    if s in i:
        l.append(i)
for i in l:
    dic.pop(i) #  或 del dic[i]
print(dic)
#输入所需删键值对中所含字母或数字:1
#{'k2': 2, 'j2': 4}

 3、修改文件

with open ('雪花','r',encoding = 'utf-8') as f,open ('雪花.bak','w',encoding = 'utf-8') as f1:
    for line in f:
        if '大白' in line:
            line = line.replace('大白','dabai')
        f1.write(line)
import os
os.remove('雪花')
os.replace('雪花.bak','雪花')

 

 

4、完整购物车

username = input('请输入注册账号:')
password = input('请输入注册密码:')
with open('C:\\Users\\Administrator\\Desktop\\Python\\t.txt', mode='w+', encoding='utf-8') as f:
    f.write("{}\n{}".format(username,password))
print('恭喜你注册成功!')
list = []
i = 0
while i <3:
    u_of_you = input("请输入您的账号:")
    p_of_you = input('请输入您的密码:')
    with open('C:\\Users\\Administrator\\Desktop\\Python\\t.txt', mode='r+', encoding='utf-8') as f:
        for line in f:
            list.append(line)
    if u_of_you ==list[0].strip()  and  p_of_you==list[1].strip():
        print('登录成功!')
        # 购物车
        li = [{'name': '手机', 'price': '1500'},
              {'name': '平板', 'price': '2000'},
              {'name': '电视', 'price': '3000'},
              {'name': '电脑', 'price': '4000'},
              ]
        money_of_you = input('请输入您拥有的金额:')
        surplus = money_of_you
        shopping_car = {}
        while 1:
            if money_of_you.isdigit() and int(money_of_you) > 0:
                for i, j in enumerate(li):
                    print('序号{},名称{},价格{}'.format(i, li[i]['name'], li[i]['price']))
                choose = input('请输入想购买的序号(若退出直接回车):')
                if choose.isdigit() and int(choose) < len(li) and int(choose) >= 0:
                    num = input('需要购买的数量:')
                    if num.isdigit() and int(num) > 0:
                        if int(surplus) > int(li[int(choose)]['price']) * int(num):
                            surplus = int(surplus) - int(li[int(choose)]['price']) * int(num)  # 剩余金额
                            if li[int(choose)]['name'] in shopping_car:  # 将相同的放一起
                                shopping_car[li[int(choose)]['name']] = shopping_car[li[int(choose)]['name']] + int(num)
                            else:
                                shopping_car[li[int(choose)]['name']] = int(num)
                            print('亲爱的{}用户,您购物车商品有{},剩余金额为{}'.format(u_of_you,shopping_car, surplus))
                        else:
                            print('余额不足!')
                            print('亲爱的{}用户,您购物车商品有{},剩余金额为{}'.format(u_of_you,shopping_car, surplus))
                            break
                    else:
                        print('输入数量有误,请重新输入。')
                        continue
                elif choose == '':
                    print('亲爱的{}用户,您购物车商品有{},剩余金额为{}'.format(u_of_you,shopping_car, surplus))
                    break
                else:
                    print('购物车中未有您输入的序号,请重新输入:')
                    continue
            else:
                print('您输入不是金额,请重新输入:')
                continue
    else:
        print('输入错误')
    i += 1

 

posted @ 2020-05-10 23:06  寂*凉  阅读(175)  评论(0编辑  收藏  举报