day2::(python-学习之路)-文件处理

需求流程图:

需求代码:

复制代码
#_*_ coding:utf-8 _*_
total_money = int(input('请输入您的预算金额:'))
total_list = []
while total_money > 0:
    f = open('shopping_list','r')
    print('''
THE SHOPPING LIST
name    money ''') #清单模板
    shopping_list= {}
    for i in f.readlines():
        (k,v) = i.strip().split()
        shopping_list[k] = v
        print(k,v) #打印购物清单
    f.close()
    min_money = int(min(shopping_list.items(),key=lambda x:x[1])[1]) #取商品的最小金额
    if total_money < min_money: #比较输入金额
        print('sorry!你的余额不足,不能购买任何商品!!Bye!~')
        break
    if total_money > 0:
        choice_name = input('请选择你要购买的商品名称:')
        total_money = total_money - int(shopping_list[choice_name])
        if total_money > 0:  #判断输入的商品金额是否超出余额
            print('您购买的商品是:%s,剩余金额为:%d' %(choice_name,total_money))
            next_time = input('是否继续购买:Y/N?')
            total_list.append(choice_name)
            if next_time == 'y':
                continue
            else:
                break
        else:
            print('您的金额不足!')
            break
print('您购买的商品为:%s' %total_list)
复制代码

 

posted @ 2017-12-01 14:06  Fighting_python  阅读(114)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示