手机销售系统(字典)

#coding:utf-8

phone_info = [{'name':'vivox9', 'price':'1200', 'count':'30'},
 {'name':'iphone6', 'price':'2000', 'count':'55'}, {'name':'iphone6s', 
'price':'2200', 'count':'120'}, {'name':'iphone7', 'price':'4000', 
'count':'80'}, {'name':'iphone7s', 'price':'4200', 'count':'90'}, 
{'name':'iphone8', 'price':'5200', 'count':'70'}]

# 定义查看所有手机品牌的函数
# 形参is_detail是bool类型的变量,如果值为True,查询手机详细信息;值为False,
查询简要信息。
def select_all_phone(is_detail):
    # xrange(len(phone_info)): 默认从索引为0的元素开始遍历。
    for x in xrange(len(phone_info)):
        # 取出每一个字典
        phone_dict = phone_info[x]
        if is_detail == True:
            print '{0}. 品牌:{1}  价格:{2}元  库存:{3}个'.format(x+1, 
phone_dict['name'], phone_dict['price'], phone_dict['count'])
        else:
            print '%s. 品牌:%s'%(x+1,phone_dict['name'])

# 定义查看某个品牌的详细信息还是进行返回
def detail_info_or_back():
    print '1-根据产品序号,查看手机详细信息'
    print '2-返回'
    select_operation = input('请选择操作编号:')
    while select_operation != 1 and select_operation != 2:
        select_operation = input('编号错误,请重新选择操作编号:')
    if select_operation == 1:
        select_number = input('请输入产品编号:')
        while select_number < 1 or select_number > len(phone_info):
            select_number = input('编号错误,请重新输入产品编号:')
        # 以编号select_number作为索引,从phone_info列表中取出对应的字典。
        phone_dict = phone_info[select_number-1]
        print '{0}. 品牌:{1}  价格:{2}元  库存:{3}个'.format(select_number,
phone_dict['name'],phone_dict['price'],phone_dict['count'])
        # 调用buy_or_bakc()函数,因为在购买手机的时候,需要将phone_dict字典中的
库存数量进行-1的操作。所以,需要将phone_dict这个字典,传入到buy_or_back()函数中去。
        buy_or_back(phone_dict)
    else:
        return

# 定义用于购买或返回的函数
def buy_or_back(phone_dict):
    print '1-购买'
    print '2-返回'
    select_number = input('请选择操作编号:')
    while select_number != 1 and select_number != 2:
        select_number = input('操作错误,请重新选择操作编号:')
    if select_number == 1:
        # 在输入购买数量之前,先要提示库存数量共多少台
        total_count = int(phone_dict['count'])
        print '当前库存共%s台!'%total_count
        buy_count = input('请输入购买数量:')
        while buy_count <= 0 or buy_count > total_count:
            buy_count = input('输入错误,请重新输入购买数量:')
        # 根据剩余数量,修改字典库存即可。
        other_count = total_count - buy_count
        phone_dict['count'] = str(other_count)
        # 当库存为0时,将该品牌手机对应的字典,从phone_info列表中进行移除。
        if int(phone_dict['count']) == 0:
            phone_info.remove(phone_dict)
        print '\n购买成功!\n'
    else:
        return

# 定义添加产品或修改产品的函数
def add_or_update_phone_info():
    print '1-添加新产品'
    print '2-修改原有产品'
    select_number = input('请选择操作编号:')
    while select_number != 1 and select_number != 2:
        select_number = input('操作错误,请重新选择操作编号:')
    if select_number == 1:
        new_phone_name = raw_input('输入新产品名称:')
        new_phone_price = raw_input('输入新产品价格:')
        new_phone_count = raw_input('输入新产品库存:')
        # 将新产品的信息,存入字典中,再将这个字典存入列表phone_info中即可。
        new_phone_dict = {'name':new_phone_name, 'price':
new_phone_price, 'count':new_phone_count}
        phone_info.append(new_phone_dict)
        print '\n添加新产品成功!\n'
    else:
        # 在修改产品信息之前,先查询所有的手机详细信息,方便选择某一个产品。
        select_all_phone(True)
        print '1-输入修改的产品编号'
        print '2-返回'
        select_number = input('请选择操作编号:')
        while select_number != 1 and select_number != 2:
            select_number = input('操作错误,请重新选择操作编号:')
        if select_number == 1:
            number = input('请输入修改编号:')
            while number < 1 or number > len(phone_info):
                number = input('编号错误,请重新输入修改编号:')
            # 获取修改后的数据
            update_name = raw_input('输入修改后的名称:')
            update_price = raw_input('输入修改后的价格:')
            update_count = raw_input('输入修改后的库存:')

            # 以数据编号number为索引,从列表phone_info中取出要修改的字典
            update_dict = phone_info[number-1]
            # 执行修改数据的操作
            update_dict['name'] = update_name
            update_dict['price'] = update_price
            update_dict['count'] = update_count

            print '\n执行修改成功!\n'
        else:
            return

# 定义删除产品或返回的函数
def delete_phone_or_back():
    print '1-根据产品编号删除产品'
    print '2-删除所有产品'
    print '3-返回'
    select_number = input('请选择操作编号:')
    while select_number < 1 or select_number > 3:
        select_number = input('请选择操作编号:')
    if select_number == 1:
        select_all_phone(True)
        number = input('请选择要删除的产品:')
        while number < 1 or number > len(phone_info):
            number = input('输入错误,请重新选择要删除的产品:')
        del phone_info[number-1]
        print '\n删除产品成功!\n'
    elif select_number == 2:
        while len(phone_info):
            del phone_info[0]
    else:
        return


while True:
    print '''
    1.查看所有手机品牌
    2.添加或修改手机信息
    3.删除手机信息
    4.退出程序
    '''
    select_number = input('请选择操作编号:')
    while select_number <= 0 or select_number > 4:
        select_number = input('操作错误,请重新选择操作编号:')
    if select_number == 1:
        select_all_phone(False)
        detail_info_or_back()
    elif select_number == 2:
        add_or_update_phone_info()
    elif select_number == 3:
        delete_phone_or_back()
    else:
        break


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值