作业:
# 1、编写文件copy工具
ANSR:
inp_src_path = input("请输入需要拷贝文件的绝对路径:>>>").strip()
inp_dst_path = input(r"请输入新文件的绝对路径及新文件名:>>>").strip()
with open(r"%s"%inp_src_path, mode="rb") as f1,open(r"%s"%inp_dst_path, mode="wb") as f2:
data = f1.readlines()
f2.writelines(data)
#2、编写简单购物车程序,自己分析逻辑,完成编程:
1、先要求用户注册
2、注册完成后,可以登录
3、登录成功后,从文件中读取商品信息(名字、价钱)展示给用户
4、用户可以选择要购买的商品和购买的个数
选做:(需要预习一下文件的修改知识点,https://www.cnblogs.com/linhaifeng/articles/5984922.html#_label5)
1、注册的时候需要输入自己的金额
2、用户选择购买商品后,减掉余额
3、其他逻辑自己任意加,想不清楚逻辑就去看看别人的购物车,开始阶段,逻辑无所谓完美,多思考就好
编程的逻辑就是人类的逻辑,多锻炼一下自己分析问题的逻辑
ANSR:
while True:
msg=''' 土包子商城登录系统
0 退出
1 登录
2 注册
'''
print(msg)
cmd = input("请输入命令:>>> ").strip()
user_normal = []
user_locked = []
if not cmd.isdigit():
print("必须输入正确的数字命令!")
continue
if cmd == "0": # 退出
break
# 登录功能,验证用户名是否存在,存在输入密码,验证完密码进入
# 登陆后可以看到商城的下一级菜单,可以选择要购买的商品和购买的个数
# 需要增加:用户选择购买商品后,减掉余额
elif cmd == "1": # 登录
tag = True
while tag:
with open("day009taskdb.txt",mode="rt",encoding="utf-8") as user_f,
open("day009tasklocked_user.txt",mode="rt",encoding="utf-8") as user_locked_f:
inp_name = input("请输入您的账号:>>>").strip()
for line_locked in user_locked_f:
name_locked, pwd_locked, balance_locked = line_locked.strip("
").split(":")
user_locked.append(name_locked)
if inp_name not in user_locked:
for line_normal in user_f:
name, pwd, balance = line_normal.strip("
").split(":")
user_normal.append(name)
if inp_name in user_normal:
# 验证成功后的代码:
print("用户名验证成功!")
count = 0
while count < 3:
inp_pwd = input("请输入您的密码:>>>").strip()
if inp_pwd == pwd:
print("登录成功,正在进入商城>>> ")
with open("day009taskgoods_list.txt",mode="rt",encoding="utf-8") as goods_list_f:
goods_list = goods_list_f.readlines()
msg_dic = {}
for goods in goods_list:
k,v = goods.strip("
").split(":")
msg_dic[k] = v
# msg_dic = dict(goods_list)
while True:
for key, value in msg_dic.items():
print("""商品名称:{0} 商品单价:{1}元""".format(key,value))
else:
print("""
我要买买买 OR 离开土包子商城
0 我走了(退出登录)
1 买买买""")
cmd_pre_buy = input("请输入命令>>> ")
if not cmd_pre_buy.isdigit():
continue
elif cmd_pre_buy == "1": ##进入购买程序,之前应添加步骤:是否开始选购,不选购就
inp_goods_name = input("请选择您要购买的商品>>> ").strip()
if inp_goods_name in msg_dic:
inp_goods_nums = input("请选择您要购买的数量>>> ").strip()
if not inp_goods_nums.isdigit():
continue
print("您购买的商品是:{0},单价:{1}元,总价{2}元".format(inp_goods_name,msg_dic[inp_goods_name],int(inp_goods_nums)*int(msg_dic[inp_goods_name])))
tag_buy_confirm = True
while tag_buy_confirm:
print("""
是否确认购买
1 确认
2 取消
""")
cmd_buy = input("请输入命令>>>: ").strip()
if not cmd_buy.isdigit():
continue
if cmd_buy == "1":
# with open()
print(inp_name,name, pwd, balance,"line102购买确认")
# pass # 确认购买,进行账户余额核减
elif cmd_buy == "2":
print("您已取消购买,请重新选购!")
break
else:
print("请输入正确的命令!")
# pass # 付款、是否继续购买等功能
else:
print("]]]]]")
else:
print("请重新输入正确的商品名称!")
elif cmd_pre_buy == "0":
print("返回上一级")
break
else:
print("请输入正确的命令!")
else:
count += 1
print("密码错误{0}次,输错三次后账号锁定,请重新输入>>> ".format(count))
continue
# 密码输错三次后执行
else:
with open("day009tasklocked_user.txt",mode="at",encoding="utf-8") as user_locked_f,
open("day009taskdb.txt",mode="rt",encoding="utf-8") as user_f:
for line_normal in user_f:
name, pwd, balance = line_normal.strip("
").split(":")
if name == inp_name:
user_locked_f.write("{0}:{1}:{2}
".format(name, pwd, balance))
print("密码已输错三次,账号已锁定,请联系管理员")
break
else:
print("账户名不存在,请重新输入账号>>> ")
else:
print("账户名已锁定,请输入合法账号>>> ")
# pass
elif cmd == "2": # 注册 验证用户名是否存在,不存在继续输入密码,存在则报错并退出到输入用户名的行
tag = True
while tag:
# inp_name = input("请输入您要注册的账号:>>>").strip()
with open("day009taskdb.txt",mode="rt",encoding="utf-8") as user_f,
open("day009taskdb.txt",mode="at",encoding="utf-8") as user_f1:
inp_name = input("请输入您要注册的账号:>>>").strip()
for line in user_f:
name, pwd, balance = line.strip("
").split(":")
if inp_name == name:
print("用户名已存在,请重新输入")
break
else:
print("用户名可用")
inp_pwd = input("请您输入密码:>>> ").strip()
re_inp_pwd = input("请您输入密码:>>> ").strip()
if not inp_pwd == re_inp_pwd:
print("两次密码不一致,请重新输入!")
continue
inp_balance = input("请输入您的注册金额:>>> ").strip()
if not inp_balance.isdigit():
print("充值金额必须是整数!")
continue
user_data = "{0}:{1}:{2}".format(inp_name,inp_pwd,inp_balance)
user_f1.writelines(user_data)
# tag = False
else:
print("输入的命令不存在")