2016-11-20 54 views
0

列表中刪除的東西所以我編寫了Python 3.5.2的清單。我的部分清單是通過在列表中鍵入事物的名稱來刪除列表中的某些東西的功能。我一直在想辦法去做,但還沒有找到。 這裏是我一起工作的代碼:如何從用戶輸入python

import time 
import random 
#Intro tells player how to use program 
def displayIntro(): 
    print("Hello there, welcome to your Python Checklist.") 
    time.sleep(2) 
    print("Please type the name of the chore e.g. rubbish.") 
    time.sleep(2) 
    print("To remove a completed chore, type ‘done’ then the name of the chore e.g. done rubbish.") 
    time.sleep(3) 
    print("It will tick the box next to the chore you said. ☑") 
    time.sleep(2) 
    print("To show unfinished chores type ‘display’ with check boxes next to them. ☐") 

#Stores user input in a list 
def nameChores(): 
    chores = '' 
    chores2 =() 
    chores3 = '' 
    chores2 = input('Type chore name: ').split() 
    print("Chore(s) is now stored.") 
    while chores != 'display' and chores in chores2: 
     time.sleep(0.5) 
     print('Make sure to look at the intro for the commands.') 
     chores = input().lower() 

#Displays the unfinished chores that the player has typed in and appear with unfilled checkboxes next to them 
    if chores == 'display': 
     print(chores2) 
    return chores 

#Program looks for chore name and removes it from the list. Says to player it is removed with a tick next to it. 
    if chores in chores2: 
     chores2.remove(chores) 
     print("Chore is now remove. ☑") 




nameChores() 
+0

這是你想要的嗎? http://stackoverflow.com/questions/2793324/is-there-a-simple-way-to-delete-a-list-element-by-value-in-python –

回答

0
import time 

currentChores = [] 
finishedChores = [] 

def displayIntro(): 
    print("Hello there, welcome to your Python Checklist.") 
    time.sleep(2) 
    print("Please type the name of the chore e.g. rubbish.") 
    time.sleep(2) 
    print("To remove a completed chore, type ‘done’ then the name of the chore e.g. done rubbish.") 
    time.sleep(3) 
    print("It will tick the box next to the chore you said. ☑") 
    time.sleep(2) 
    print("To show unfinished chores type ‘display’ with check boxes next to them. ☐") 

def nameChores(): 
    chore = input("Type chore name: ") 

    if (chore == "display"): 
     print(currentChores) 

    elif ("done " in chore): 
     subString = chore.replace("done ", "") 
     if subString in currentChores: 
      currentChores.remove(subString) 
      finishedChores.append(subString) 
      print("Chore: " + subString + " was removed from your chore list") 
     else: 
      print("The chore: " + subString + " doesn't exist in your chore list") 

    elif (chore not in currentChores): 
     currentChores.append(chore) 
     print("Chore: " + chore + " was added to your chore list") 






def main(): 

    nameChores() 
main() 

該代碼段將執行displayChores介紹,然後進行不斷:由在該nameChores函數添加瑣事到currentChores列表無限循環

如果chore添加兩次,chore將從currentChores列表中刪除,並添加到finishedChores列表,在這裏你可以輕鬆地訪問所有菲尼棚chores

你可以很容易使腳本停止,每當你想通過修改nameChores功能:

def nameChores(): 
    while True: 
     chore = input("Type chore name: ") 

     if (chore == "display"): 
      print(currentChores) 

     elif (chore == "stop"): 
      break 

     elif ("done " in chore): 
      subString = chore.replace("done ", "") 
      if subString in currentChores: 
       currentChores.remove(subString) 
       finishedChores.append(subString) 
       print("Chore: " + subString + " was removed from your chore list") 
      else: 
       print("The chore: " + subString + " doesn't exist in your chore list") 

     elif (chore not in currentChores): 
      currentChores.append(chore) 
      print("Chore: " + chore + " was added to your chore list")