python从零学习day-2 homework:购物车小程序

python从零学习day-2 homework:购物车小程序

购物车小程序
1、启动程序后,让用户输入工资,然后进入循环,打印商品列表和编号

2、允许用户根据商品编号选择商品

3、用户选择商品后,检测余额是否够,够就直接扣款,并加入购物车, 不够就提醒余额不足

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

//
goods = [
    ('电脑',990),('笔',50),('英语书',90),('鼠标',20),('Ipad',650)]
wage = input('请输入你的工资(输入“q"以随时退出购物):')
while wage == 'q' or wage =='Q':
    print('欢迎下次光临')
    exit()
while not wage.isdigit():
    wage=input("输入有误,请重新输入:")
wage = int(wage)
print("商品列表")
shopping_cart = []
for i in goods:
    print(goods.index(i) + 1, i[0])
while True:
    consumer_choice = input("\033[31m请输入你选择的商品编码:\033[0m")
    if consumer_choice.isdigit():
        consumer_choice = int(consumer_choice)
        if 0 < consumer_choice <= len(goods):
            if goods[consumer_choice-1][1]<=wage:
                x=[goods[consumer_choice-1][0],goods[consumer_choice-1][1]]
                shopping_cart.insert(-1,x)
                wage-=goods[consumer_choice-1][1]
                print('您的余额为%d'%wage)
            else:
                print('您的余额为%d,请重新选购商品:' %wage)
        else:
            print('输入有误,清重新输入')
    elif consumer_choice == 'q' or consumer_choice =='Q':
        break
    else:
        print('请输入数字格式的商品编码')

print('------消费记录------')
for i in shopping_cart:
    print(shopping_cart.index(i)+1,i[0],i[1])
print('您的余额为%d'% wage)
print('谢谢惠顾!')

你可能感兴趣的:(python入门练习)