码迷,mamicode.com
首页 > 其他好文 > 详细

instance4:购物车优化版 (作业)

时间:2018-01-24 22:14:30      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:ice   分享图片   高亮   strong   字典   优化   显示   gis   购物车   

购物车程序:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Deakin"
# Email: 469792427@qq.com
# Date: 2018/1/17

product_list=[
    ["iphone",8000],
    ["computer",6000],
    ["car",30000],
    ["coffee",30],
    ["watch",500],
    ["book",80],
]
shopping_list=[]
file=open(user,r+,encoding=utf-8)
f=eval(file.read())
file1=open(history,r+,encoding=utf-8)
f1=eval(file1.read())
account=input("pls key in your account:")
while True:
    if account in f:
        password=input("pls key in your password:")
        if password in f[account]:
            salary=int(f[account][password])
            print(welcome %s,your balance salary is %s%(account,salary))
            print(-----below is your shopping car history-----)
            if account in f1:
                historylist=f1[account]
            else:
                historylist=[]
            print(historylist)
            while True:
                select=input(do you want to buy sth now?yes or no:)
                if select==yes:
                    while True:
                        for index,item in enumerate(product_list):
                            print(index,item)
                        userchoice = input("pls select the product number,press ‘q‘ to quit:")
                        if userchoice.isdigit():
                            userchoice=int(userchoice)
                            if userchoice<len(product_list) and userchoice>=0:
                                price=product_list[userchoice]
                                if price[1]<salary:
                                    confirm=input(pls confirm your selection,yes or no:)
                                    if confirm==yes:
                                        shopping_list.append(price)
                                        salary-=price[1]
                                        print(add \033[31;1m%s\033[0m in your shopping_list,your \033[31;1mbalance salary is %s\033[0m%(price,salary))
                                        f[account][password]=str(salary)
                                        file.seek(0)
                                        file.write(str(f))
                                        file.tell()
                                    elif confirm==no:
                                        pass
                                    else:
                                        print(invalid option)
                                else:
                                    print("you don‘t have enough money")
                        elif userchoice==q:
                            print("------shopping list------")
                            for p in shopping_list:
                                print(p)
                            f1[account]=shopping_list
                            file1.seek(0)
                            file1.write(str(f1))
                            print(your repeated salary is %s%(salary))
                            exit()
                        else:
                            print(option invalid)
                elif select==no:
                    exit()
                else:
                    print(invalid option)
        else:
            print("password is incorrect,pls try again")
    else:
        newpwdsal={}
        password_new=input("account don‘t exist,pls set up your password to register:")
        salary_new=input("first time to log in, pls key in your salary:")
        sal_new=int(salary_new)
        newpwdsal[password_new]=sal_new #工资对应新密码
        f[account]=newpwdsal #用户名对应密码和工资的小字典
        file.seek(0)
        file.write(str(f))
        file.tell()

 

 技术分享图片

技术分享图片

 

instance4:购物车优化版 (作业)

标签:ice   分享图片   高亮   strong   字典   优化   显示   gis   购物车   

原文地址:https://www.cnblogs.com/Deakin-Du/p/8343158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!