2012-04-16 62 views
0

分配isPhase 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() 
    

這是我寫的所有代碼到目前爲止有什麼想法嗎?我試圖刪除的代碼是如果mangchoice == 2:

+3

不要交叉發佈問題。你問過關於程序員的同樣的問題。如果問題被認爲是可以接受的,它將由主持人遷移到適當的地點。 – 2012-04-16 01:24:52

+0

您將希望使用[''with''語句](http://docs.python.org/reference/compound_stmts.html#the-with-statement)打開文件。它處理你的結束和例外,並且讀得很好。 – 2012-04-16 01:30:23

+0

對不起@MikeL。我不知道,第一次發佈,我認爲我應該這樣做,根據上一篇文章的回覆來判斷。再次抱歉不會再發生。 – bradb 2012-04-16 01:31:55

回答

0

在Python3中,input返回一個字符串。它看起來像你需要將它轉換爲int

namedelete = int(input("what item would you like to remove")) 
相關問題