python week2

"""
购物车程序:
1. 程序初始化输入工资
2. 输入商品编号
3. 判断余额,余额不足退出
"""

g_list = [["0","水果Xr","8999"],["1","小米8","2699"],["2","魅族16","3299"],["3","VIVO NEX","4999"],["4","荣耀Magic2","5999"],["5","OPPO Findx","4999"]]
s_list = []
Salary = int(input("Please input your salary:"))

while True:
    for i in g_list:
        print(i)
    choice = int(input("Please input your choice:"))
#判断用户输入在商品范围
    if choice in range(len(g_list)):#判断余额
        if Salary >= int(g_list[choice][2]):
            s_list.append(g_list[choice])#加购物车
            Salary -= int(g_list[choice][2])
            print("Add %s to shopping cart!,your balance is %s" %(g_list[choice][1],Salary))
        else:
            print("Shopping list:")#余额不足打印购物车
            for j in s_list:
                print(j)
            print("your balance is %s" %Salary)
            exit()
#返回重新输入选项
    else:
        print("Wrong choice!")

  

posted @ 2018-09-20 00:26  nale  阅读(149)  评论(0)    收藏  举报