Python自动化3.0-------学习之路-------购物车程序!

购物车程序:

1、运行程序
2、输入用户名和密码,第一次输入,需要输入工资
3、登陆成功后,是否打印历史购物清单和剩余的工资
4、选择需要购买的商品
5、按q退出程序,保存已购的商品列表。

 

 

#! /usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Mego
set = False
file =open('user','r+',encoding='utf-8')
f =str(file.read())
for line in f:
    file_str=str(f)
data=eval(file_str)
name=input("登陆名:")
password=input("密码:")
while True:
    if name in data:
        if password in data[name]:
            salay=float(data[name][password])
            print('''\033[32;1m欢迎登陆,您的余额为:%s\033[0m'''%salay)
            break
        else:
            password=input("密码错误,请重新输入:")
            continue
    else:
        password_salay={}
        salay_str=input("请输入你的工资:")
        salay=float(salay_str)
        password_salay[password]=salay
        data[name]=password_salay
        file.seek(0)
        file.write(str(data))
        file.tell()
        break
list =[
    ["iphone",5800],
    ["bike",800],
    ["mac pro",7000],
    ["book",75],
    ["apple",5]
]
file_list_r=open('history','r+',encoding='utf-8')
f_list_r=str(file_list_r.read())
shoppinglist_dict=eval(f_list_r)
if name not in shoppinglist_dict[name]:
        shoppinglist_dict[name]=[]

shoppinglist = shoppinglist_dict[name]
shoppinglist_now=[]
choose =input("\n是否查询历史购物车记录(y/n):")
if choose=='y':
    print("-----------历史购物车记录-----------")
#    shoppinglist = shoppinglist_dict[name]
    print( name,shoppinglist)
    print("--------------结束-------------")
while not set:
    print("----------shopping list----------")
    for index,item in enumerate(list,1):
        print(index,item)
    print("--------END---------")
    number=input("请输入你想购买的商品编号:")
    if number=="q":
        set = True
        data[name][password]=str(salay)
        file.seek(0)
        file.write(str(data))
        file.tell()
        print("--------购物清单---------")
        print(shoppinglist_now)
        print("您的余额为:",salay)
        print("---------END-------")
#        shoppinglist.extend(shoppinglist_now)
        shoppinglist_now.append(shoppinglist)
        shoppinglist_dict[name]=shoppinglist
        file_list_r.seek(0)
        file_list_r.write(str(shoppinglist_dict))
        file_list_r.tell()
    elif number.isdigit()==False:
        print("\033[31;1m输入不是编号内容,请重新输入\033[0m")
    elif int(number)>int(len(list)) or int(number)<=0:
        print("\033[31;1m你所购买的商品不在商品清单里面\033[0m")
    else:
        number_buy=int(number)-1
        if list[number_buy][1]<(salay):
            salay=salay-int(list[number_buy][1])
            msg = '''
            \033[32;1m您已经将%s加入了购物车中,余额为%d\033[0m
            '''%(list[number_buy][0],salay)
            print(msg)
            shoppinglist_now.append(list[number_buy])

        else:
            print("\033[31;1m您已经没有约可以购买!\033[0m")

  

posted @ 2017-07-24 10:49  Mego_Mateng  阅读(211)  评论(0)    收藏  举报