0

Ok so I've wrote a code and used if statement and if statement is not doing what its supposed to do its just keep executing the fist case even if i type anything elsenything else.

while True:
    user_input = input("Type add, show, edit, completed or exit: ")
    user_input: str = user_input.strip()

    if 'add' or 'Add' in user_input:
        todo = user_input[4:] + '\n'

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

        todos.append(todo)

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

    elif 'show' or 'Show' in user_input:

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

        # todos_without_breakline = [item.strip('\n') for item in todos]

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

    elif 'edit' or 'Edit' in user_input:
        numbere = int(input("Enter the number of todo to edit: "))
        new_todo = input("Enter the new todo: ")

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

        todos[numbere - 1] = new_todo + "\n"

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

    elif 'completed' or 'Completed' in user_input:
        numberc = int(input("Enter the number of todo that is completed: "))

        with open('files/todos.txt', 'r') as file:
            todos = file.readlines()
        index = numberc - 1
        todo_to_remove = todos[index].strip('\n')
        todos.pop(index)

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

        message = f"Todo '{todo_to_remove}' is removed from the list"
        print(message)

    elif 'exit' or 'Exit' in user_input:
        break
    else:
        print("Unknown Command")

print("Bye")

Heres the thing when i get prompted i try to add todos by typing "add Pay chess". Ok thats done but now if i type "show" i still don't get anything where i should have printed the whole todo file. and heres another thing if you type "show Play chess" IT WILL ADD "PLAY CHESS: TO THE TODO LIST. means no matter what i type it just keeps executing the fist AKA add case. I dont know what to do so to be very honest i havent tried anything accept removing "or" in cases and converting it into if only statement.

Plaese help.

5P33DC0R3

5P33DC0R3
  • 21
  • 2
  • `if 'add' or 'Add' in user_input` is always `True`. `or` doesn't work like that, it doesn't distribute values across an operator like `in`. It's two separate conditions: `'add'` (which is always truthy because it's a non-zero length string) `OR 'add' in user_input'` (which is never checked because the first condition is true). Write them as separate `in` tests with `or` between them. – kindall Aug 16 '23 at 17:59
  • better yet, don't check for variations in case this way. `'add' in user_input.lower()` works regardless of casing. – kindall Aug 16 '23 at 18:00
  • @kindall see below ;) – JRiggles Aug 16 '23 at 18:03
  • I got answers and it realy helped so now ive changed `user_input = input("Type add, show, edit, completed or exit: ")` to `user_input = input("Type add, show, edit, completed or exit: ").lower()` and removed **or add, etc...** – 5P33DC0R3 Aug 16 '23 at 18:09
  • This is a combination of [this problem](https://stackoverflow.com/q/15112125/12479639) and one of [these solutions](https://stackoverflow.com/questions/6159313/how-to-test-the-membership-of-multiple-values-in-a-list). – Axe319 Aug 16 '23 at 18:16

1 Answers1

1

You cant do if 'add' or 'Add' in user_input:, you have to separate the checks, e.g.

if 'add' in user_input or 'Add' in user_input:

But it's probably better to use

if 'add' in user_input.lower()

to make it case-insensitive.


All that said, it may be better still to use something like

if user_input.lower() == 'add':  # or 'edit' or whatever

so the condition doesn't trigger if the user enters something spurious like 'fiddlefaddle' (which has 'add' in it)

JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • or even `if user_input in ('add', 'ADD'):` if poster insists on all uppercase or all lowercase. – Frank Yellin Aug 16 '23 at 18:09
  • @FrankYellin, yeah, checking a list of options also works. But it's still probably better to just compare everything in the same case so you don't have to worry about `('add', 'Add', 'aDd', 'adD', 'ADd', 'AdD, 'ADD')` etc. hahah. Never trust the user to respect the input format you want – JRiggles Aug 16 '23 at 18:13
  • I missed `'aDD'` but you get the point – JRiggles Aug 16 '23 at 18:21