欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

我的第一个Python程序

程序员文章站 2024-03-23 19:22:22
...

第一次写博客,有一丢丢的羞涩,有一丢丢的激动,哈哈,完全的一个小白,都不知道要写一些啥,今天闲来无聊,写了一个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()



相关标签: Python