zoukankan      html  css  js  c++  java
  • python程序2:购物车

    主要使用的python的if,while,for循环语句来实现。需要注意循环语句中的结构体。
    1、初级版,主要使用循环。

    python购物车
     
    #!/usr/bin/env python3
    #_*_ encoding:utf8_*_
    #__author__:wang
    product = [
        ('oppo',3000),
        ('hasee',8000),
        ('bike',800),
        ('python book',80),
        ('coffee',20)
    ]
    salary=input("pls input your salary:")
    shopping_list = []       #购物车
    if salary.isdigit():      #判断输入的是否为整数
        salary=int(salary)      #强行转换为整数
        while True:      #死循环
            for index,item in enumerate(product):     #函数enumerate会自动生成index和item,方便后续增加商品
                print(index,item)          #打印索引和商品
            user_choice=input("select buy>>>>")
            if user_choice.isdigit():            #判断输入的值是否为整数
                user_choice=int(user_choice)
                if user_choice < len(product) and user_choice>=0:          #判断是否在范围内
                    p_item=product[user_choice]
                    if p_item[1] <= salary:                     #判断是否金额足够  
                        shopping_list.append(p_item)       #加入购买车
                        salary -= p_item[1]
                        print("your buy product 33[31:1m %s 33[0m,only 33[31:1m %s 33[0m rmb" %(p_item,salary))
                    else:
                        print("your salary only 33[31:1m %s 33[0m rmb,don't buy this prod33[31:1m %s 33[0m" %(p_item,salary))
                else:
                    print("the prod isn't exist.")
            elif user_choice == "q":
                print("you buy prod:".center(50,'-'))
                for p in shopping_list:
                    print(p)
                print("you only salary33[32;1m %s 33[0m rmb" %(salary))
                exit()
            else:
                print("input error.")
      
    2、购物车的增强版 使用python文件来操作,并记录用户输入的工资,第二次打开也有记录
  • 相关阅读:
    记录——framework探测定位程序集与core探测定位程序集
    C# 特定框架适用特定代码
    python读取excel代码
    时间比较
    ORA 01791错误
    MongoDB.1什么是MongoDB
    Mayatis 异常之result maps collection already contains value...
    怎样做好黄焖鸡
    关于foreach
    C#之out,ref关键字
  • 原文地址:https://www.cnblogs.com/wang50902/p/14280144.html
Copyright © 2011-2022 走看看