2015-06-16 181 views
-2

上的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()在追加或刪除功能的選擇()函數將無限次地調用append。 那麼在追加或刪除列表中的數據後,如何恢復選擇?

+0

把無所不在的'選擇'放到無限'while'循環中。 –

+0

你能解釋何時以及如何調用append()或remove()'嗎? – ASCIIThenANSI

+0

'def choices():'
'append(x)'ofcourse我有整個正確的代碼格式。 @ASCIIThenANSI –

回答

1

如tobias_k所示,您應該將choices()的內容添加到while循環中。 我還發現 一些其他問題:

  • False不等於"False",所以你while循環永遠不會運行。
  • 您可以使用術語如mylistmylist1mylist2 - 最好是這些重命名爲choosing_listappending_list,並且removing_list,所以它更清晰。
  • 你也不應該使用False來定義一個while循環 - 相反,創建一個變量,然後將其設置爲True。當你必須停止時,將其設置爲False。

這裏是固定這些問題的代碼:

def appending(appending_list = []): 
    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: 
      appending_list.append(c) 
    print("You have inserted {} choices".format(len(appending_list))) 
    print("Please verify them below: ") 
    for x in range(0, len(appending_list)): 
     print(appending_list[x]) 
    return appending_list 

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

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

讓我知道是否有與此代碼的任何問題。

+0

真的很感激!我是初學者!你的代碼對我幫助很大!謝啦 –