我的第一个Python程序

第一次写博客,有一丢丢的羞涩,有一丢丢的激动,哈哈,完全的一个小白,都不知道要写一些啥,今天闲来无聊,写了一个Python小程序,现在正在苦恼如何优化,如果有路过的大神,请指点一下,谢谢啦大笑

print('--------------欢迎购物----------------')

# 设置商品列表
commodity_list = [
    ['MacBook', 6000],
    ['MacBook Pro', 18000],
    ['Iphone X', 8800],
    ['HCK 23.6 1800R', 750],
    ['SamSung S9+', 6699],
    ['HUAWEI Mate10', 3999],
    ['HUAWEI Mate10 pro', 4599]
]

while True:
    # 输入工资
    salary = input('Please enter your salary:')
    shopping_list = []
    # 判断输入的字符串是否为数字
    if salary.isdigit():
        # 转换salary数据类型
        salary = int(salary)
        while True:
                # 输出商品列表
                for commodityNum, commodityInfo in enumerate(commodity_list):
                    print(commodityNum, commodityInfo)
                # 输入操作指令
                shopping_choice = input('''
                    Enter Number buy the commodity
                    Enter B return to the previous menu
                    Enter Q exit system
                ''')
                # 判断输入操作指令类型
                if shopping_choice.isdigit():
                    shopping_choice = int(shopping_choice)
                    # 判断输入商品代码是否合理
                    if shopping_choice >= 0 and shopping_choice < len(commodity_list):
                        # 获取商品价格
                        commodity_prices = commodity_list[shopping_choice]
                        # 判断资金是否足够购买商品
                        if salary >= commodity_prices[1]:
                            shopping_list.append(commodity_list[shopping_choice])
                            salary -= commodity_prices[1]
                            print('''
                                    Good purchased:{list}
                                    Balance:{salary}
                            '''.format(list=shopping_list, salary=salary))
                        else:
                            print('Not sufficient funds!')
                            continue
                    else:
                        print("Goods don't exist!")
                        continue
                elif shopping_choice == 'B':
                    break
                elif shopping_choice == 'Q':
                    print('''
                            Good purchased:{list}
                            Balance:{salary}
                    '''.format(list=shopping_list, salary=salary))
                    exit()
                else:
                    print('Input error!')
    else:
        # 告知输入的工资错误,询问是否继续
        print('')
        result = input('''
            The input salary is wrong!
            Want to continue? Yes enter Y , No enter N
        ''')
        if result == 'N':
            exit()



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值