购物车程序需求:

代码如下:

#coding=utf-8

salary=input('请输入工资:')
goods=[['iphone',5800],['book',30],['bike',800]] #商品列表
shopping_list=[] #购物车列表

if salary.isdigit(): #判断是否数字
    salary=int(salary)
    while True:
        for index,item in enumerate(goods): #枚举可同时获得索引和值
            print(index,item)
        user_choice = input('请选择编号:')
        if user_choice.isdigit():
            user_choice=int(user_choice)
            if user_choice<len(goods) and user_choice>=0:
                p_item=goods[user_choice]
                if p_item[1]<=salary: #买的起
                    shopping_list.append(p_item)
                    salary-=p_item[1]
                    print('商品%s已加入购物车,当前余额为\033[32;1m%s\033[0m'%(p_item[0],salary)) #余额高亮绿色32
                else:
                    print('余额为\033[31;1m%s\033[0m 不足支付,请另选商品,或按q退出'%salary)#余额高亮红色31

        elif user_choice=='q':
            print('-------shopping list-------')
            for p in shopping_list: #打印购物车列表
                print(p)
            print('你的当前余额为\033[31;1m%s\033[0m'%salary)
            exit() #退出

        else:
            print('无效操作')

更多相关文章

  1. 教您使用java爬虫gecco抓取JD全部商品信息

随机推荐

  1. Android 更新UI的两个方法
  2. android Application类的详细介绍
  3. Android NDK报错(Eclipse)及解决方法
  4. 使用迅雷代替SDK Manager快速下载Android
  5. 《转载》Android(安卓)AlertDialog 方法s
  6. 【Android】16.5 Android内置的系统服务
  7. android 加载动态库
  8. Android 中文api (88)――SharedPreferen
  9. Android(安卓)Model正确使用姿势——Auto
  10. android中调用接口发送短信