0

分配是阶段 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,我试图删除的代码如下:

4

1 回答 1

0

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

namedelete = int(input("what item would you like to remove"))
于 2012-04-16T01:36:50.090 回答