[英]How to remove something from a list that is made from user input python
所以我正在用Python 3.5.2编写清单。 我的清单的一部分是通过在列表中键入事物名称来删除列表中事物的功能。 我一直在试图找到一种方法来做,但是还没有找到。 这是我正在使用的代码:
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()
此代码段将执行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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.