作业day09

2020-06-11

一、编写文件copy工具

inp_txt = input("请输入被复制的文件名:").strip()
inp_name = input("请拟定复制的新文件名称:").strip()
with open(r'%s' % inp_txt, mode='rb') as f1, open(r'%s' % inp_name, mode='ab') as f2:
    data = f1.readlines()
    f2.writelines(data)

二、编写简单购物车程序,自己分析逻辑,完成编程:

1、先要求用户注册
2、注册完成后,可以登录
3、登录成功后,从文件中读取商品信息(名字、价钱)展示给用户
4、用户可以选择要购买的商品和购买的个数

while True:
    tag = True
    while tag:
        user_name = input("请注册您的用户名:").strip()
        with open(r'registration.txt', mode='rt', encoding='utf-8') as f1:
            l = []
            for line in f1:
                name, *_ = line.strip('
').split(':')
                l.append(name)
            if user_name not in l:
                while tag:
                    user_psw = input('请注册您的密码(仅由数字组成):')
                    if user_psw.isdigit():
                        with open(r'registration.txt', mode='at', encoding='utf-8') as f2:
                            f2.write("%s:%s
" % (user_name, user_psw))
                            tag = False
                    else:
                        print('您输入的密码格式不正确,请重新设置密码')
            else:
                print("您输入的用户名已存在,请重新设置用户名")
    del l, tag
    i = 0
    while i < 3:
        inp_name = input("请输入您的用户名:")
        inp_psw = input("请输入您的密码:")
        with open(r'registration.txt', mode='rt', encoding='utf-8') as f3:
            for line in f3:
                name, psw = line.strip('
').split(':')
                if inp_name == name and inp_psw == psw:
                    print("登录成功!")
                    i = 3
                    break
            else:
                print("登录失败,请重新登录!")
                i += 1
    del i
    with open(r'shopping.txt', mode='r', encoding='utf-8') as f4:
        print(f4.readlines())
        while True:
            inp_commodity = input("请输入你需要购买的商品:").strip()
            l1 = []
            for line in f4:
                name, *_ = line.strip('
').split(':')
                l.append(name)
            if inp_commodity not in l1:
                print("您输入的商品名不存在,请重新输入!")
            else:
                while True:
                    num = input("请输入你需要的数量:")
                    if num.isdigit():
                        num = int(num)
                        break
原文地址:https://www.cnblogs.com/cui-cheng/p/13096934.html