关于使用Python——写运用函数和字典完成手机销售系统

方法一

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。如果is_detail的值为True,查询手机详细信息。如果值为False,查询简要信息
def select_all_phone(is_detail):
    for x in range(0,len(phone_info)):
        #取出每一个字典
        phone_dict=phone_info[x]
        #is_detail是True,则查询详细信息(手机品牌,价格,库存)
        if is_detail==True:
            print('{0}.品牌{1},价格{2},库存{3}'.format(x+1,phone_dict['name'],phone_dict['price'],phone_dict['count']))
        #如果is_detail不是True,那就是False,查询简要信息(手机品牌)
        else:
            print('{0}.品牌{1}'.format(x+1,phone_dict['name']))


#定义查看某个品牌的详细信息或者返回的一个函数
def detail_info_or_back():
    print('1-根据产品序号查看手机详细信息')
    print('2-返回')
    #根据控制台输入的数字,来进行判断运行哪个选项
    select_operation=int(input('请输入你要操作的序号:'))
    #循环检测用户输入的序号是否符合要求
    while select_operation!=1 and select_operation!=2:
        select_operation = int(input('输入错误,请重新输入你要操作的序号:'))
    #如果用户选择的是序号1,则运行第一个功能选项
    if select_operation==1:
        select_all_phone(False)
        select_number=int(input('请输入要查询的手机详细信息的手机品牌序号:'))
        #循环检测用户输入的手机品牌序号是否正确。
        while select_number<1 or select_number>len(phone_info):
            select_number=int(input('输入的序号有误,请重新输入要查询的手机详细信息的手机品牌序号:'))
        phone_dict=phone_info[select_number-1]
        print('{0}:品牌{1},价格{2},库存{3}'.format(select_number,phone_dict['name'],phone_dict['price'],phone_dict['count']))

        #函数里面嵌套函数,因为用户选择序号1的时候,下面还有购买或者返回的操作,把购买和返回的操作单独进行函数封装。最后当用户选择1的时候,之间在序号1这个选择中,调用个buy_or_back()函数。
        buy_or_back(phone_dict)

    #如果选择的不是序号1,那么就是序号2,返回。
    else:
        return


#定义一个购买或者返回的函数,设置一个参数接收某手机的字典信息
def buy_or_back(phone_dict):
    print('1-购买')
    print('2-返回')
    select_number=int(input('请选择要操作的编号:'))
    #循环检测用户是否输入错误
    while select_number!=1 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值