我试图从列表中删除一个字符串,然后将其写回文件

时间:2012-04-16 01:22:50

标签: python python-3.x

作业是第1阶段 - 管理模式
当用户选择管理模式时,应允许他们执行以下操作:

  1. 将新产品添加到列表中(还必须添加多少项目 和它的成本)
  2. 从列表中删除产品
  3. 更改项目数量

    def mang (grocerystock):
    
    mangchoice=int(input("What would you like to do? \n 1):Add a new product to the list? \n 2): Remove a product from the list?  \n 3: Change the quantity of an item  \n 4): Change the price of an item  \n  5): View items and their quantity and price"))
    
    if mangchoice == 1:
        infile=open("grocery_stock.txt", 'a')
        name=input("Please enter the new product's name would you like to add:")
        quant=int(input("Please enter the new product's quantity"))
        price=float(input("Please enter the new product's price"))
        grocerystock[0]=name
        grocerystock[1]=quant
        grocerystock[2]=price
        gS=str(grocerystock)
        gs=gS.strip("[',']")
        infile.write(gs + '\n')
    if mangchoice == 2:
        namedelete=input("what item would you like to remove")
        a=open("grocery_stock.txt", 'r')
        data_list= a.readlines()
        a.close()
        print (data_list)
        del data_list[namedelete]
        b= open ("grocery_stock.txt", 'w')
        b.writelines(data_list)
        b.close()
    def intro():
    choice=(int(input("Would you like to go to Managerial mode or Shop mode?(press 1 for Managerial and 2 for shop mode, to quit press 3)")))
    
    
    if choice == 1:
        print ('lets go')
        mang(grocerystock)
    elif choice == 0 :
        print ('loser')
    
    grocerystock= ["","",""]
    
    intro()
    
  4. 这是我到目前为止所写的所有代码?我想删除的代码如果是mangchoice == 2:

1 个答案:

答案 0 :(得分:0)

在Python3中,input返回一个字符串。看起来您需要将其转换为int

namedelete = int(input("what item would you like to remove"))