Python练习购物菜单

要求:
1.展现商品菜单及其价格
2.输入欲采购的总金额
3.输入商品名称进行采购,如果钱足够购买价格最低商品,则可一直购买,否则提示余额不足,是否要进行充值,如果进行充值则总金额增加,可以继续购物。
4.在开始部分要求对输入进行判断,必须为整形int,否则报错重新输入
5.在选择物品清单的时候,可以输入“?”号进行帮助,帮助内容包括list清单(当前购买的商品都有哪些,且中途充值了一共多少钱,当前余额还剩下多少。)、back(返回上一级菜单)、quit(退出系统)
这里写图片描述

import sys
item_list = ['aa','bb','cc','dd','ee','ff','gg','hh'] #商品列表
price_list = [5,8,6,3,1,12,24,990]  #商品价格
purchase_list = [] #购物车
def latest_list():     
    for ab in purchase_list:
        print ab #打印购物车清单
dict()
amount_money = 0
one = 0
for i in item_list: 
    print i,price_list[item_list.index(i)] #打印商品及商品价格
while True:
    try:
        salary = int(raw_input("please input your salary:").strip())
        break   #确定输入为整形
    except ValueError:
        print ("please input number,not string!")
        continue
while one == 0:
    select_item = raw_input("please input your select item name below:").strip()
    if select_item == '?':
        help_action = raw_input("\033[32mlist  Display all of your purchase list.\n"
                                "back  Return to the higher level menu.\n"
                                "quit  Exit this purchase system.\n\033[0m"
                                "Please input your decide:").strip()
        if help_action == 'list':
            latest_list()
            print 'Your add top-up amount is:',amount_money
            print 'Your remaining amount is now:',salary
            continue
        elif help_action == 'back':
            one == 1
        elif help_action == 'quit':
            sys.exit()
    elif select_item == 'quit':
        sys.exit()
    for a in item_list:
        if a == select_item:
            purchase_list.append(a)   #将买入的商品加入到购物车
            salary = salary - price_list[item_list.index(a)]  #剩余金额
            if salary < min(price_list):
                last = raw_input("your money is not enough to buy another something! Will you to top-up (yes/no)?").strip()
                if last == 'yes':
                    top_money = int(raw_input("please input your top-up money:").strip())
                    amount_money += top_money
                    salary = salary + top_money  #充值金额
                    one == 1
                elif last == 'no':
                    print ("Thank you ! byebye~~")
                    sys.exit()
            elif salary >= min(price_list):
                continue
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值