Ask coding questions

← Back to all posts
if statements not working correctly, please help

my code

all it does is prints

form the quit message in the first if statement.
Is it something from the with open(file)... thing?

Answered by ruiwenge2 [earned 5 cycles]
View Answer

That’s because you used the quit function. remove them


thx, it worked i just had to remove the if message.lower() == "quit" or "exit":` ...


@Ravost I can help! You need to understand the proper use of the or in Python.

If you look at if message.lower() == "quit" or "exit":, it would be re-syntaxed like this:

if (message.lower() == "quit") or "exit":

Which "exit" would return True (un-boolean things return True unless for 1) no matter what, which would result in the whole if statement returning True because you used or. So, to fix it, you do:

if (message.lower() == "quit") or (message.lower() == "exit"):

Which would be the correct solution. Hope this helps, and hope you can mark it as answered!


@CodingElf66 also works, i just removed the quit function, but thanks anyway!


@Ravost But by the way if you want the system to quit for real, you can do my strategy. Because if you went with @ruiwenge's, all that you do is if the user inputs exit or quit, it just does nothing. So hope this helps and you can mark it as answered!