简体   繁体   中英

How to remove something from a list that is made from user input python

So I am coding a checklist in Python 3.5.2. Part of my checklist is the function that is removing something in the list by typing the name of the thing in the list. I have been trying to figure out a way to do it but have not found one. Here is the code that I am working with:

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()
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()

This code snippet will execute the displayChores intro, and then proceed to continuously add chores to the currentChores list by having the nameChores function in a infinite loop

If a chore is added twice, the chore will be removed from the currentChores list and added to the finishedChores list, where you easily can access all finished chores

You can easily make the script stop whenever you want by modifying the nameChores function:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM