列表数据结构在python中的应用

列表数据结构在python中的应用,python,data-structures,Python,Data Structures,祝你有一个非常快乐和成功的一天。 我现在正在学习python中的高级数据结构,并试图通过这个关于使用列表处理事情的练习找到答案。请帮我纠正这个错误。问题如下: " 创建一个程序,允许用户(1)向列表中添加产品,(2)删除项目,(3)打印列表并退出 如果用户向列表中添加了某些内容,程序会询问“将添加什么?”:“并将其保存为列表中的最后一项。如果用户决定删除某些内容,程序会通知用户列表中有多少项(列表中有[number]项),并提示用户输入删除的项(“删除了哪些项?”))。如果用户选择0,则删除第一

祝你有一个非常快乐和成功的一天。 我现在正在学习python中的高级数据结构,并试图通过这个关于使用列表处理事情的练习找到答案。请帮我纠正这个错误。问题如下:

" 创建一个程序,允许用户(1)向列表中添加产品,(2)删除项目,(3)打印列表并退出

如果用户向列表中添加了某些内容,程序会询问“将添加什么?”:“并将其保存为列表中的最后一项。如果用户决定删除某些内容,程序会通知用户列表中有多少项(列表中有[number]项),并提示用户输入删除的项(“删除了哪些项?”))。如果用户选择0,则删除第一项。当用户退出时,将为用户打印最终列表“列表中保留以下项目:”然后是每行一个剩余项目。如果用户选择了选项之外的任何内容,包括删除项目时,程序将响应“选择不正确”。当程序正常工作时,它会打印出以下内容:

Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Apples
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Beer
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Carrots
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 2
There are 3 items in the list.
Which item is deleted?: 3
Incorrect selection.
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 2
There are 3 items in the list.
Which item is deleted?: 2
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 2
There are 2 items in the list.
Which item is deleted?: 0
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 4
Incorrect selection.
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 3
The following items remain in the list:
Beer
我的实施

mylist=[]
while True:
    n=int(input("Would you like to\n(1)Add or\n(2)Remove items or\n(3)Quit?: "))
    if n==1:
        a=input("What will be added?: ") 
        mylist.append(a)
    if n==2:
        count=len(mylist)
        print("There are ",count," items in the list.")
        b=int(input("Which item is deleted?: "))
        for b in range(len(mylist)):
            mylist.pop(b)
        else:
            print("Incorrect selection.")
    if n==3:
        print("The following items remain in the list:\n", mylist)
        break
然后程序打印出如下内容:

Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Apples
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Beer
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Carrots
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 2
There are  3  items in the list.
Which item is deleted?: 3
Traceback (most recent call last):
  File "ohjelma.py", line 12, in <module>
    mylist.pop(b)
IndexError: pop index out of range
您想继续吗
(1) 添加或
(2) 删除项目或
(3) 退出?:1
将添加什么?:苹果
你愿意吗
(1) 添加或
(2) 删除项目或
(3) 退出?:1
要加什么?:啤酒
你愿意吗
(1) 添加或
(2) 删除项目或
(3) 退出?:1
将添加什么?:胡萝卜
你愿意吗
(1) 添加或
(2) 删除项目或
(3) 退出?:2
列表中有3项。
删除了哪个项目?:3
回溯(最近一次呼叫最后一次):
文件“ohjelma.py”,第12行,在
mylist.pop(b)
索引器:弹出索引超出范围
我不知道我捕捉到了哪些错误。请帮我做这个!!!
谢谢你的时间和努力

列表中的索引从0开始。在你的例子中,胡萝卜的指数是2。索引3不存在,无法从列表中弹出。for循环似乎不起作用,您只需检查输入是否小于列表的长度。
n==2
的逻辑应该是这样的:

Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Apples
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Beer
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 1
What will be added?: Carrots
Would you like to
(1)Add or
(2)Remove items or
(3)Quit?: 2
There are  3  items in the list.
Which item is deleted?: 3
Traceback (most recent call last):
  File "ohjelma.py", line 12, in <module>
    mylist.pop(b)
IndexError: pop index out of range
如果n==2:
计数=len(mylist)
打印(“列表中有”,计数,“项目”)
b=int(输入(“删除了哪个项目:”)
如果b<计数:
mylist.pop(b)
其他:
打印(“选择不正确”)