python3.5模拟购物车程序

要求:python

一、启动程序后,输入用户名密码后,让用户输入工资,而后打印商品列表git

二、容许用户根据商品编号购买商品 app

三、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒code

四、可随时退出,退出时,打印已购买商品和余额 blog

 

思路:input

一、index,和for循环商品列表it

二、商品号要在商品序列内io

三、判断余额for循环

四、打印所选商品及价格class

goods =[
{"name":
"电脑", "price": 1999},
{"name":
"鼠标", "price": 10},
{"name":
"游艇", "price": 20},
{"name":
"美女", "price": 998},

]
shopping_list = []
wage = input('请输入你的工资:')

if wage.isdigit():
    wage=int(wage)

while True:
    for index,i in enumerate(goods):
        print(index,i)
    user_choice = input('请选择要买的编号>>:')
    if user_choice.isdigit():
        user_choice = int(user_choice)
        if user_choice < len(goods) and user_choice >=0:
            goods_item = goods[user_choice]
            print(goods_item['price'])
            if goods_item['price'] < wage:#买的起
                shopping_list.append(goods_item)
                wage -=goods_item['price']
                print("添加 %s 到你的购物车,你的余额为 \033[31;1m%s\033[0m" %(goods_item,wage) )
            else:
                    print("\033[41;1m你的余额不足[%s]啦\033[0m" % wage)
        else:
                print("product code [%s] is not exist!"% user_choice)
    elif user_choice == 'q':
            print("--------shopping list------")
            for p in shopping_list:
                print(p)
            print("你的余额为:",wage)
            exit()
    else:
            print("invalid option")
相关文章
相关标签/搜索