[Python] output is not what it is supposed to be

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    i am making todo app or program on python in which output is not working according to the code

    while True:

    user_action = input("type add, show, edit, remove or exit ")
    user_action = user_action.strip()


    if 'add'in user_action:
    todo = user_action[4:]

    with open('todos.txt', 'r') as file:
    todos = file.readlines()

    todos.append(todo)


    with open('todos.txt', 'w') as file :
    file.writelines(todos)

    elif 'show'in user_action:

    with open('todos.txt', 'r') as file :
    todos = file.readlines()

    #new_todos = [item.strip("\n") for item in todos]

    for index, items in enumerate(todos):
    items = items.strip("\n")
    index = index + 1
    row = f"{index}-{items}"
    print(row)


    elif 'edit' in user_action:
    number = int(user_action[5:])
    print(number)
    number = number - 1

    with open('todos.txt', 'r') as file:
    todos = file.readlines()

    new_todo = input("Type the new todo:")
    todos[number] = new_todo + "\n"

    with open('todos.txt', 'w') as file :
    file.writelines(todos)




    elif 'remove' in user_action:
    number = int(user_action[6:])
    with open('todos.txt', 'r') as file:
    todos = file.readlines()
    index = number - 1
    todo_to_remove = todos[index].strip("\n")
    todos.pop(index)
    with open('todos.txt', 'w') as file :
    file.writelines(todos)
    message = f"todo {todo_to_remove} was removed from the list"
    print(message)


    elif 'exit' in user_action:
    break
    else:
    print("Command is not valid ")

    print("bye!")


    `

    hey this is the code for my program but the problem is when i am adding input "add anything" and after that i am asking the program to show my todos then it is not showing me the correct output

    type add, show, edit, remove or exit add bro
    type add, show, edit, remove or exit show
    1-ab
    2-cd
    3-hihellohikrrish
    4-hi
    5-
    6-hiibro
    type add, show, edit, remove or exit add broski
    type add, show, edit, remove or exit show
    1-ab
    2-cd
    3-hihellohikrrish
    4-hi
    5-
    6-hiibrobroski


    this is the output i received , the output of show should be

    1-ab
    2-cd
    3-hihellohikrrish
    4-hi
    5-
    6-hii
    7-bro
    8-broski

    Continue reading...

Compartilhe esta Página