本帖最后由 qq1151985918 于 2021-5-23 17:23 编辑 class Task(object):
def __init__(self):
self.task_urgent = []
self.task_normal = []
self.run = True
def show_task(self):
print("\n紧急任务列表:", *self.task_urgent, sep = "\n--")
print("\n普通任务列表:", *self.task_normal, sep = "\n--")
def add_task(self):
print("\n提示:")
in_task = input("请输入任务,格式如(紧急:去买菜!):")
while in_task[:2] not in ["紧急", "普通"]:
print("输入错误!请重新输入任务,格式如(紧急:去买菜!):")
in_task = input("请输入任务:")
if in_task[:2] == "紧急":
if in_task not in self.task_urgent:
self.task_urgent.insert(0, in_task)
else:
print("任务已存在!")
if in_task[:2] == "普通":
if in_task not in self.task_normal:
self.task_normal.insert(0, in_task)
else:
print("任务已存在!")
def delete_task(self):
print("\n任务编号:")
for x in range(len(self.task_urgent)):
prompt = "--紧急%d:" % x
print(prompt,self.task_urgent[x])
print()
for y in range(len(self.task_normal)):
prompt = "--普通%d:" % y
print(prompt,self.task_normal[y])
task_index = input("请输入要删除的任务编号(如紧急1):")
while task_index[:2] not in ["紧急", "普通"]:
print("输入错误!请重新输入要删除的任务编号(如紧急1):")
task_index = input("请输入要删除的任务编号:")
if task_index[:2] == "紧急":
if int(task_index[2:]) in range(len(self.task_urgent)):
del self.task_urgent[int(task_index[2:])]
else:
print("任务编号不在范围!")
if task_index[:2] == "普通":
if int(task_index[2:]) in range(len(self.task_normal)):
del self.task_normal[int(task_index[2:])]
else:
print("任务编号不在范围!")
def exit_task(self):
print("\n程序退出!")
self.run = False
def to_do(self):
print("\n指令名称:list, add, delete, exit")
cmd = input("请输入指令:")
while cmd.lower() not in ["list", "add", "delete", "exit"]:
cmd = input("指令输入错误,请重新输入:")
cmd = cmd.lower()
if cmd == "list":
self.show_task()
if cmd == "add":
self.add_task()
if cmd == "delete":
self.delete_task()
if cmd == "exit":
self.exit_task()
if __name__ == "__main__":
t = Task()
while t.run:
t.to_do()
|