Iris_soyaa 2021-11-14 21:04 采纳率: 50%
浏览 44
已结题

python 列表添加,删除,搜索,改变

img


求解答,已经琢磨了好久了

  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-11-14 23:18
    关注

    代码可这样写:

    def main():
        global numberList
        numberList = [2, 0, 2, 1, 0, 7, 0, 8, 1, 3, 1, 4]
        while True:
            print('1.Add the number to the list and return.\n2.Delete the number in the list and return.\n3.Check if the number is in the list and return True or False.\n4.Change the value  of the list and return.\n0.quit')
            option=input('please input a option(0,1,2,3,4):')
            if option=='1':
                print(f'numberList:{add_ele()}')
            elif option=='2':
                print(f'numberList:{del_ele()}')
            elif option=='3':
                print(check_ele())
            elif option=='4':
                print(f'numberList:{change_ele()}')
            elif option == '0':
                break
    def add_ele():
        global numberList
        x=int(input('Input a number to add:'))    
        numberList.append(x)
        return numberList
    def del_ele():
        global numberList
        x=int(input('Input a number to delete:')) 
        for a in numberList:
            if a==x:
                numberList.remove(a)
        return numberList
    def check_ele():
        global numberList
        x=int(input('Input a number to search:'))
        if x in numberList:
            return True
        else:
            return False
    def change_ele():
        global numberList
        x=int(input('Input a number to change to 0:'))
        new=[]
        for a in numberList:
            if a==x:
                new.append(0) 
            else:
                new.append(a)
        return new
    main()
    

    如有帮助,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月6日
  • 已采纳回答 11月28日
  • 创建了问题 11月14日