2016-11-20 7 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が二回追加した場合、無限ループ

chorecurrentChoresリストから削除され、あなたは簡単にすべてのfiniのにアクセスすることができfinishedChoresリストに追加されますあなたがnameChores機能を変更することによって、いつでもchores

を当てるあなたは簡単にスクリプトの停止を行うことができます。

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") 
関連する問題