Skip to content
Sign upLog in
← Back to Community

if statements not working correctly, please help

Profile icon
Crowst

my code

#varibles and imports import colors, os, random, sys, time from swears import bad_words #import login from replit import db, clear user = os.environ['REPL_OWNER'] owners = ['ravost'] def printc(color, text): print(f"colors.{color}{text}") message = input(f"{colors.orange}Type your message.{colors.reset} ") while True: clear() with open('chat.txt', 'r') as chat: print(chat.read()) if message.lower() == "quit" or "exit": with open('chat.txt', 'a') as chat: chat.write(f"{colors.red}{user} left the chat\n") chat.close(); quit() elif message.lower() in bad_words: print(f"{colors.red}@{user} Don't swear!") time.sleep(1) quit() clear() else: with open('chat.txt', 'a') as chat: chat.write(f"{colors.reset}{user}: {message}\n") chat.close() clear()

all it does is prints

repl process died unexpectedly:

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
Voters
Profile icon
dxmi
Profile icon
Crowst
Comments
hotnewtop
Profile icon
ruiwenge2

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

Profile icon
Crowst

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

Profile icon
CodingElf66

@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!

Profile icon
Crowst

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

Profile icon
CodingElf66

@Ravost
Ok!

Profile icon
CodingElf66

@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!