1、编写文件copy工具
original = input ( r"请输入原文件名: " ) . strip( )
new = input ( r"请输入新文件名: " ) . strip( )
with open ( "%s" % original, mode= "rt" , encoding= "utf-8" ) as f1, \
open ( "%s" % new, mode= "wt" , encoding= "utf-8" ) as f2:
for line in f1:
f2. write( line)
2、编写简单购物车程序,自己分析逻辑,完成编程:
1、先要求用户注册
2、注册完成后,可以登录
3、登录成功后,从文件中读取商品信息(名字、价钱)展示给用户
4、用户可以选择要购买的商品和购买的个数
选做:(需要预习一下文件的修改知识点,https://www.cnblogs.com/linhaifeng/articles/5984922.html#_label5)
1、注册的时候需要输入自己的金额
2、用户选择购买商品后,减掉余额
3、其他逻辑自己任意加,想不清楚逻辑就去看看别人的购物车,开始阶段,逻辑无所谓完美,多思考就好
编程的逻辑就是人类的逻辑,多锻炼一下自己分析问题的逻辑
while True :
msg = """
1 退出
2 登录
3 注册
4 购物
"""
print ( msg)
cmd = input ( '请输入命令编号>>>: ' ) . strip( )
if not cmd. isdigit( ) :
print ( '必须输入命令编号的数字,傻叉' )
continue
if cmd == '1' :
break
if cmd == '2' :
name = input ( "请输入用户名: " ) . strip( )
pwd = input ( "请输入密码: " ) . strip( )
with open ( "user.txt" , mode= "rt" , encoding= "utf-8" ) as f:
lst = f. readlines( )
for i in lst:
username, password = i. strip( ) . split( ":" )
if name == username and pwd == password:
print ( "登录成功" )
break
else :
print ( "账号或密码错误" )
elif cmd == "3" :
name = input ( "请输入用户名: " ) . strip( )
pwd = input ( "请输入密码: " ) . strip( )
with open ( "user.txt" , mode= "at" , encoding= "utf-8" ) as f:
f. write( "%s:%s\n" % ( name, pwd) )
elif cmd == '4' :
productlist = [ ]
pricelist = [ ]
sum = 0
tag = True
while tag:
with open ( "fruit.txt" , mode= "rt" , encoding= "utf-8" ) as f:
lst = f. readlines( )
for i in lst:
product, price = i. strip( ) . split( ":" )
productlist. append( product)
pricelist. append( price)
print ( lst)
print ( "开启买买买之旅,退出可按q" . center( 50 , '*' ) )
pro = input ( "请输入购买商品名: " ) . strip( )
if pro in productlist:
num = input ( "请输入购买个数: " ) . strip( )
quit = input ( "按enter键继续,按q退出: " ) . lower( )
number = productlist. index( pro)
total = int ( pricelist[ number] ) * int ( num)
print ( "商品:%s\n数量:%s\n总价:%s\n" % ( pro, num, total) )
if quit == "q" :
with open ( "shopping.txt" , mode= "a+" , encoding= "utf-8" ) as f:
f. seek( 0 , 2 )
f. write( pro + ":" + num + "\n" )
with open ( "shopping.txt" , mode= "r" , encoding= "utf-8" ) as f:
lst = f. readlines( )
print ( lst)
sum += total
print ( lst)
print ( "共计:%s元" % ( sum ) )
tag = False
break
else :
print ( "非法输入,重新输" )
else :
print ( '输入的命令不存在' )