Q:

Why does my Python code keep returning "Invalid mode." with proper input?

So I'm making a simple timer program to get better at Python since I've never really tried it before. With my limited knowledge of the language, I don't feel I'm qualified to diagnose this issue myself. I tried changing double quotes to single quotes around my "if mode ==" statements, but that didn't change anything. I keep getting "Invalid mode.", which is my error message for when it's not up or down.
Here is my code:
import time

seconds = 0
mode = "down"

def main():
    print("Timer v1.0 by Alexis Knox")
    time.sleep(1)
    seconds = int(input("Seconds to count: "))
    mode = str(input("Timer mode [up/down]: "))

    if mode != 'up':
        print("Invalid mode.")
        exit(0)
    if mode != 'down':
        print("Invalid mode.")
        exit(0)

    if seconds < 1:
        print("Invalid time.")
        exit(0)

    if mode == 'up':
        for x in range(seconds):
            x += 1
            print(str(x))
            time.sleep(1)

    if mode == 'down':
        y = seconds
        for y in range(seconds):
            y -= 1
            print(str(y))
            time.sleep(1)

# Ensure that we execute main
if __name__ == "__main__":
    main()

A:

If you look at what I have done here to your code this should fix the problem
if seconds < 1:
    print("Invalid time.")
    exit(0)

elif mode == 'up':
    for x in range(seconds):
        x += 1
        print(str(x))
        time.sleep(1)

elif mode == 'down':
    y = seconds
    for y in range(seconds):
        y -= 1
        print(str(y))
        time.sleep(1)
else:
    print("Invalid mode.")
    exit(0)

If you are running consecutive if statements as you were previously it is better to use the elif statement if they are relevant to each other as shown in my code. The else is also more efficient as this will do exactly what the first part of your previous code was doing except it will run the if and elif tests first. The else will basically catch anything that is not stated in the elif and if tests. How ever your output is probably not what you want it to be but that was not the issue

