zoukankan      html  css  js  c++  java
  • python 第12天作业

    #写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作
    import os
    name = input('document:')
    context = input('context:')
    fixcontext = input('fixcontext:')
    def my_replace(name,context,fixcontext):
    with open(name,'r',encoding='utf-8') as read_f,
    open('name_swap','w',encoding='utf-8') as write_f:
    for line in read_f.readlines():
    write_f.write(line.replace(context,fixcontext))
    os.remove(name)
    os.rename('name_swap',name)
    my_replace(name, context,fixcontext)
    #写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。
    def notnone(*args):
    for i in args:
    if len(i) == 0:
    print(type(i),'为空')
    else:
    for j in i:
    if len(str(j)) == 0:
    s = j
    print('%s中的有%s个元素为空' % (i,i.count(s)))
    s = ''
    l = [1,2,'','','',4,5]
    t = ('',)
    notnone(s,l,t)
    # #写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    # dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    # PS:字典中的value只能是字符串或列表
    def cut_dic_value(**kwargs):
    for i in kwargs.keys():
    if len(kwargs[i]) > 2:
    kwargs[i] = kwargs[i][:2]
    return kwargs
    dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    print(cut_dic_value(**dic))

    #用函数实现购物车程序
    def login():
    num = 0
    while True:
    usr = input('user:')
    pwd = input('password:')
    if usr == 'egon' and pwd == '123':
    print('login success welcome',usr)
    return True
    else:
    print('login fail')
    if num > 2:
    return False
    num += 1
    def purch():
    usr_goods = []
    goods = {'apple':5,'pear':3,'tomato':6,'egg':2}
    while True:
    for key, value in goods.items():
    print('商品:{name} 价格:{price}'.format(name=key, price=value).center(30, '#'))
    select = input('请选择商品')
    if select not in goods:
    print('没有此商品')
    continue
    else:usr_goods.append({select:goods[select]})
    while True:
    num = input('请输入数量:')
    if num.isdigit():
    num = int(num)
    usr_goods[-1]['num'] = num
    print('购买成功')
    break
    else:
    print('非法输入,请的输入数字')
    continue
    ex = input('是否继续购买?1继续,2结算')
    if ex == '1':continue
    else:break
    return usr_goods

    if login():
    print(purch())
  • 相关阅读:
    一个网站架构的变迁
    网络编程
    http协议篇
    第1篇 编程能力是什么
    django中的cookies和session机制
    django的认证与授权系统
    python的异常处理
    第0篇
    mysql优化和全局管理杂记
    k8s中pod的资源配置详解
  • 原文地址:https://www.cnblogs.com/fenglin0826/p/7240803.html
Copyright © 2011-2022 走看看