Python语言学习:homework1

'''
购物车程序
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4.可随时退出,退出时,打印已购买商品和余额
'''
#!/usr/bin/env python
goods_list=[
    ('cloth',200),
    ('rice',80),
    ('coffee',25),
    ('software',25000),
    ('computer',10000),
    ('TVGames',300),
]
shopping_list=[]
salary = input("your salary is:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,intem in enumerate(goods_list):
            print(index,intem)
        UserChoice = input ("your choice is:")
        if UserChoice.isdigit():
            UserChoice = int(UserChoice)
            if UserChoice <len(goods_list) and UserChoice>=0:
                p_item = goods_list[UserChoice]
                if p_item[1]<=salary:
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Add %s into shopping card, your current balance is %s"%(p_item,salary))
                else:
                    print("your balance is %s not enough to buy goods" %(salary))
            else:
                print("goods code[%s] is not exist"%(UserChoice))
        elif UserChoice=='q':
            print("---shopping_list---")
            for p in shopping_list:
                print(p)
            print("your current balance is ",salary)
            exit()
        else:
            print("invalid option")

  

posted @ 2020-02-05 10:21  黑球西西  阅读(120)  评论(0编辑  收藏  举报