코드

# todo list CLI
# list, file read write, looping, if else..

# 할일 추가
def add_todo():
    # todo.txt 에서 리스트로 읽어 온다
    todo_list = read_todo()
    # 리스트에 할일 추가 : 추가할 할일을 입력받는다.
    todo = input("할일을 입력해주세요 : \n")
    todo_list.append(todo)
    # 리스트를 todo.txt에 저장
    write_todo(todo_list)
    print(f"{todo}가 추가되었습니다. \n")

# 할일 삭제
def del_todo():
    # todo.txt 에서 리스트로 읽어 온다
    todo_list = read_todo()
    # 리스트에 할일 삭제 : 삭제할 할일을 입력받는다.
    todo = input("삭제할 일정을 입력해주세요 \n")
    if todo in todo_list:
        todo_list.remove(todo)
        # 리스트를 todo.txt에 저장
        write_todo(todo_list)
        print(f"{todo}가 삭제되었습니다. \n")
    else:
        print(f"{todo}가 존재하지 않습니다. \n")

# 할일 출력
def print_todo():
    # todo.txt 에서 리스트로 읽어 온다
    todo_list = read_todo()
    # 리스트를 출력
    for todo in todo_list:
        print(f"-{todo}")

# 할일을 파일에 쓰기
def write_todo(todo_list):
    with open('todo.txt', 'w', encoding='utf-8') as file:
        for todo in todo_list:
            file.write(todo + '\n')

# 할일을 파일에서 읽기
def read_todo():
    with open('todo.txt', 'r', encoding='utf-8') as file:
        todo_list = []
        for todo in file.readlines():
            todo_list.append(todo.strip())
    return todo_list

while True:
    print("다음 명령을 입력해 주세요( add, del, print, exit) \n")
    cmd = input()
    if cmd == 'add':
        add_todo()
    elif cmd == 'del':
        del_todo()
    elif cmd == 'print':
        print_todo()
    elif cmd == 'exit':
        print('종료합니다. \n')
        break
    else:
        print('그 따위 명령어는 없습니다. \n')

플로차트

+ Recent posts