返回python中的main函数

时间:2015-06-16 10:14:55

标签: python-3.x function

使用Python 3.4.3
让我们说我创造了三个功能:

def choosing(mylist=[]):
    print("We will have to make a list of choices")
    appending(mylist)
    done = False
    while(done == "False"):
        confirm = input("Is your list complete?[Y/N]")
        if(confirm == "Y"):
            print("Yaay! Choices creation complete."
                  "{} choices have been added successfully".format(len(mylist)))
            done = True
        elif(confirm == "N"):
            action = input("What do you want to do? [Append/Delete]")
            if(action == "Append"):
                appending(mylist)
                done = False
            elif(action == "Delete"):
                removing(mylist)
                done = False

def appending(mylist1 = []):
    print("Please type EOF when you want to stop!")
    while True:
        c = input("Please enter EOF to stop adding. Please enter a choice: ")
        if(c=="EOF"):
            break
        else:
            mylist1.append(c)
    print("You have inserted {} choices".format(len(mylist1)))
    print("Please verify them below: ")
    for x in range(0, len(mylist1)):
        print(mylist1[x])

def removing(mylist2 = []):
    print("Following are choices: ")
    r = input("What do you want to remove? ")
    mylist2.remove(r)
    print("{} successfully removed!".format(r))

现在的问题是我无法在追加或删除函数中调用choices(),因为choices()函数将无限次地调用append。 那么如何在列表中追加或删除数据后重新选择?

1 个答案:

答案 0 :(得分:1)

正如tobias_k所建议的那样,您应该将choices()的内容添加到while循环中。 我也找到了 其他一些问题:

  • False不等于"False",因此您的while循环永远不会运行。
  • 您使用mylistmylist1mylist2等字词 - 最好将这些字词重命名为choosing_listappending_list和{ {1}},所以它更清楚。
  • 您也不应该使用False来定义while循环 - 而是创建一个变量,然后将其设置为True。必须停止时,将其设置为False。

以下是解决了这些问题的代码:

removing_list

如果此代码存在任何问题,请与我们联系。