如何在从一开始的枚举中删除列表中的项目?

huangapple go评论52阅读模式
英文:

How to remove an item from a list with enumeration starting at one?

问题

if main == 'remove':
for count, item in enumerate(grocery_list, 1):
print(f'{count}. {item}')
which_item = input('Which item do you want to remove? Type in the name of the item please! ')
del grocery_list[int(which_item) - 1]
print('Your item has been removed!')

英文:
if main == 'remove':
    for count, item in  enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = input('Which item do you want to remove? Type in the name of the item please! ')
    del grocery_list[int(which_item-1)]
    print('Your item has been removed! ')
    continue

I'm trying to let user remove an item by typing in the enumerated index. When they do type in remove, it give them a list like this:

  1. item
  2. item
  3. item

I tried to do del grocery_list[int(which_item-1)] but that gave an error. I want one subtracted from the which_item variable.

答案1

得分: 0

If position of item is asked to the user:

if main == 'remove':
    for count, item in enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = int(input('Which item do you want to remove? Type in the position of the item please! '))
    grocery_list.pop(which_item-1)
    print('Your item has been removed!')

If value of item is asked to the user:

if main == 'remove':
    for count, item in enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = input('Which item do you want to remove? Type in the name of the item please! ')
    grocery_list.remove(which_item)
    print('Your item has been removed!')
英文:

If position of item is asked to the user:

if main == 'remove':
    for count, item in enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = int(input('Which item do you want to remove? Type in the position of the item please! '))
    grocery_list.pop(which_item-1)
    print('Your item has been removed! ')

If value of item is asked to the user:

if main == 'remove':
    for count, item in enumerate(grocery_list, 1):
        print(f'{count}. {item}')
    which_item = input('Which item do you want to remove? Type in the name of the item please! ')
    grocery_list.remove(which_item)
    print('Your item has been removed! ')

答案2

得分: 0

Sure, here is the translated code:

首先,你的用户输入值 which_item 可能在转换为整数时引发错误。因此,请将其放在 try except 块中。

其次,为什么要逐行显示你的物品并在显示所有物品之前询问用户要删除什么?将这两个过程分开。

以下是完整的代码:

grocery_list = ['item1', 'item2', 'item3', 'item4']
# 初始显示列表
for count, item in enumerate(grocery_list, 1):
    print(f'{count}.{item}')

while True:
    try:
        which_item = int(input('你想删除哪个项目?请输入项目的名称! '))
        del grocery_list[int(which_item - 1)]
        print('你的项目已被删除!', '当前列表')
        for count, item in enumerate(grocery_list, 1):
            print(f'{count}.{item}')
    except:
        break

请注意,我已经将代码部分保留为英文,如你要求的那样。

英文:

firstly your user input value which_item may raise an error during conversion into int. so keep that inside a try except block

second why would you display your items line by line and ask user what to delete before all items have even been shown? separate both of the processes

here is the complete code

grocery_list=['item1','item2','item3','item4']
#initial display of list
for count,item in enumerate(grocery_list,1):
    print(f'{count}.{item}')

while True:
    try:
        which_item = int(input('Which item do you want to remove? Type in the name of the item please! '))
        del grocery_list[int(which_item-1)]
        print('Your item has been removed! ','current list')
        for count,item in enumerate(grocery_list,1):print(f'{count}.{item}')
    except:
        break

huangapple
  • 本文由 发表于 2023年4月13日 22:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定