instance1:shopping cart(attached flow diagram)

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

#程序:购物车程序

#需求:

#启动程序后,让用户输入工资,然后打印商品列表
#允许用户根据商品编号购买商品
#用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
#可随时退出,退出时,打印已购买商品和余额

product_list=[
    ("iphone",8000),
    ("computer",6000),
    ("car",30000),
    ("coffee",30),
    ("watch",500),
    ("book",80),
]
shopping_list=[]
salary=input("pls key in your salary:")
if salary.isdigit():
    salary=int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        userchoice=input("选择商品编号,按q退出:")
        if userchoice.isdigit():
            userchoice=int(userchoice)
            if userchoice<len(product_list) and userchoice>=0:
                price=product_list[userchoice]
                if price[1]<=salary:
                    choice=input("pls confirm if you want to buy:yes or no:")
                    if choice=="yes":
                        shopping_list.append(price)
                        salary-=price[1]
                        print("added %s into your shopping list,balance is %s"%(price,salary))
                    elif choice=="no":
                        pass
                    else:
                        print("usage:key in "yes" or "no")
                else:
                    print("你钱不够")
            else:
                print("商品不存在")
        elif userchoice=="q":
            print("----------shopping list---------")
            for p in shopping_list:
                print(p)
            print("your balance is %s"%(salary))
            exit()
        else:
            print("invalid options")

 

posted @ 2018-01-05 17:21  Deakin-DJ  阅读(118)  评论(0编辑  收藏  举报