首页 > 代码库 > 购物小程序2

购物小程序2

技术分享

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Er fei

wage=input("请输入工资:")
wage=wage.strip()
has_purchase = []
if wage.isdigit():#如果输入是一个数字就执行下边代码块,
    wage=int(wage)
    print("欢迎来到苹果商城".center(40,"*"))
    exit_flag=False
    while not exit_flag:
        goods_list = [["苹果4s", 800],
                      ["苹果5", 1500],
                      ["苹果5s", 3000],
                      ["苹果6", 5000],
                      ["苹果6s", 6000],
                      ["苹果7", 8000]]
        print("商品列表".center(46,"-"))
        for indix,i in enumerate(goods_list):#enumerate,循环打印出列表的下标
            print(indix,".",i[0],i[1])
        buy_what=input("输入商品序号购买商品,输入q退出,输入a打印已购列表....请输入:")
        if buy_what.isdigit():
            buy_what=int(buy_what)#转换为int类型
            if buy_what<6:#列表商品没有那么多
                if goods_list[buy_what][1]<=wage:#判断能不能买得起
                    wage-=goods_list[buy_what][1]#结账
                    has_purchase.append(goods_list[buy_what])#加入已购商品
                    print(has_purchase)
                    print("您的余额还有%s元"%wage)
                else:
                    print("账户余额不足请充值")
            else:
                print("您输入的商品不存在请重新输入")
        elif buy_what=="a":
            print("已经购买的商品有%s,账户余额还剩%s$"%(has_purchase,wage))

        else:
            exit_flag=True

else:
    print("输入的格式不正确请重新输入")

 

购物小程序2