没搞明白fileinput模块的inplace!

__author__ = 'sophi'
import fileinput
product_list = []
f = open("porduct.txt","r",encoding="utf-8") #打开文件
for line in f.readlines():
    line = line.strip()
    index,item = line.split(":") #冒号分割
    product_list.append((index,item))
print(product_list) #存入列表
f.close()

def print_product_list():
    for index,item in enumerate(product_list):
        print(index,item)

def user_shoping():
    salary = input("请你输入你的薪水:")
#    print_product_list()
    if salary.isdigit(): #判断输入的是不是数字
        salary = int(salary)

        print_product_list()
        shopping_list = [] #存放购买的商品,购物清单
        while True:
            option = input("喜欢那个就买那个(对应的标号):")
            if option.isdigit():
                option = int(option)
                if option >= 0 and option < len(product_list):
                    p_item = product_list[option] #用户选择的商品 列表
                    print("退出请输入q或Q。")
                    c_num = int(p_item[1])
                    if salary >= c_num:
                        shopping_list.append(p_item)
                        salary -= c_num
                        print("添加购物车成功,你的余额还有%s" %(salary))
                    else:
                        print("你的余额不足,只剩下%s元" %(salary))
                else:
                    print("输入错误,请重新输入")
                    print_product_list()
            elif option == "q":
                print("-------------您的购物清单---------------")
                for s_list in shopping_list:
                    print(s_list)
                print("你的余额为%s" %salary)
                print("........exit.........")
                exit()
            else:
                print("你输入的薪水无效,请重新输入")

#print(len(product_list))
#user_shoping() #用户入口

def add_product():
    print_product_list() #打印一下以前的列表
    name_of_product = input("请输入你要添加的商品名称:")
    price_of_product = input("请输入你要添加商品的价格:")
    f = open("porduct.txt","a",encoding="utf-8")
    f.write(str("\n"+name_of_product)+": %s"%(price_of_product))
    f.close()
    print_product_list()
    print("添加成功!\n exit------")

# add_product() #添加商品

def change_price():
    print_product_list() #
    choice = input("请输入你的选择:")
    if choice.isdigit():
        choice = int(choice)
        if choice >= 0 and choice <= len(product_list):
            c_item = product_list[choice]
            print(c_item)
            change_item = input("请输入要修改的,货品和价格用逗号分隔")
            change_item = str(change_item).split(",")
#            print(change_item[0])
#            print(change_item[1])
            for line in fileinput.input("product.txt",backup='.bak',inplace=1):
                line = line.replace("%s" %(p))

    # name_of_change = input("请输入你要改商品的名称:")
    # price_of_change = input("请输入你要改商品的价格:")
#     if choice.isdigit():
#         choice = int(choice)
#         if choice >= 0 and choice <= len(product_list):
#             p_item = product_list[choice]
# #            print(p_item) #看一下格式
#             for line in fileinput.input("product.txt",inplace = "%s" %(choice)):
#                 line = line.replace("%s" %(p_item[1]),"%s" %(price_of_change)).strip()
#                 print(line)
#             exit("修改成功!")
#         else:
#             print("输入无效!")
#     else:
#         if choice == "q" or choice == "Q":
#             exit("退出")

#change_price()

  没搞明白fileinput模块的inplace!

猜你喜欢

转载自www.cnblogs.com/armyz6666666/p/9038506.html