zoukankan      html  css  js  c++  java
  • 购物车程序练习

     购物车程序需求:

    代码如下:

    #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已加入购物车,当前余额为33[32;1m%s33[0m'%(p_item[0],salary)) #余额高亮绿色32
                    else:
                        print('余额为33[31;1m%s33[0m 不足支付,请另选商品,或按q退出'%salary)#余额高亮红色31
    
            elif user_choice=='q':
                print('-------shopping list-------')
                for p in shopping_list: #打印购物车列表
                    print(p)
                print('你的当前余额为33[31;1m%s33[0m'%salary)
                exit() #退出
    
            else:
                print('无效操作')
    

      

    执行效果:

  • 相关阅读:
    继承-方法重写
    继承2
    继承
    JAVA-基本数据类型与引用数据类型区别
    JS判断一个数是否为质数
    Yslow
    Sublime Less 自动编译成css
    chrom调试
    解决在微信中部分IOS不能自动播放背景音乐
    常用的jq插件
  • 原文地址:https://www.cnblogs.com/feiyueNotes/p/8597728.html
Copyright © 2011-2022 走看看