# python Chatroom/Codehelp through the terminal

I created a system that allows individuals to ask questions about code, and chat. I wanted to make this system smaller and better because this is easy to bypass for some reason.

The modules/packages I used for this chatroom project

import os
import time


Now the actual script that contains about 56 lines of code!

print("Codehelp || Question through a terminal.")
#### Codehelp Variables
chat = open('publiclogs.txt','r')
crown = "👑"
owner = False
#### Codehelp Scripts
if name == "":
print("\nINVALID! Name must not be Empty..\n")
time.sleep(2)
exit()
while True:
with open('publiclogs.txt','a') as chat_txt:
chat_txt.close()
chat = open('chat.txt','r')
message = input("Enter Your message: ")
os.system("clear")
if message.lower() == "":
os.system("clear")
print("You must type something in order to send a message\n")
input("Press enter to continue ")
os.system("clear")
title = input("Enter title of Question. Also be specific: ")
description = input("Enter description: ")
tags = input("Enter TAGS. Seperate by ',': ")
os.system("clear")
with open('publiclogs.txt','a') as chat_txt:
chat_txt.write(f'New Question has been asked! View question.txt!\n')
chat_txt.close()
time.sleep(2)
print("Removed from Codehelp Currently..")
else:
if owner == True:
with open('chat.txt','a') as chat_txt:
chat_txt.write(f'{crown} {name}:{message}\n')
chat_txt.close()
else:
with open('chat.txt','a') as chat_txt:
chat_txt.write(f'{name}: {message}\n')
chat_txt.close()


What could I do to improve this code?

• How is this a chat room? Is the file mapped to an NFS mount or something? Jun 1 at 0:49

I have carefully read all of your code and I would like to propose some changes, in line with the PEP8 good practice convention:

• ideally, the indentation is a multiple of 4;
• it is not necessary to use multiple '#' to comment a line;
• boolean expressions like if owner == True can be simplified to if owner;
• On line 27, I replaced Seperate with Separate;
• You can replace ------------------- with {"-" * 20};
• It is also possible to avoid over-repeating ask_txt.write by inserting newline after \n;
• 1 blank space after each comma;
• 1 blank paragraph at the end of everything.

Here is the entire code including the modifications I suggested:

print("Codehelp || Question through a terminal.")
# Codehelp Variables
chat = open('publiclogs.txt', 'r')
crown = "👑"
owner = False
# Codehelp Scripts
if name == "":
print("\nINVALID! Name must not be Empty..\n")
time.sleep(2)
exit()
while True:
with open('publiclogs.txt', 'a') as chat_txt:
chat_txt.close()
chat = open('chat.txt', 'r')
message = input("Enter Your message: ")
os.system("clear")
if message.lower() == "":
os.system("clear")
input("You must type something in order to send a message\n"
"Press enter to continue ")
os.system("clear")
title = input("Enter title of Question. Also be specific: ")
description = input("Enter description: ")
tags = input("Enter TAGS. Separate by ',': ")
os.system("clear")
f'DESCRIPTION: {description}\n'
f'TAGS: {tags}\n'
f'Written by {name}\n'
f'{"-" * 20}')
with open('publiclogs.txt', 'a') as chat_txt:
chat_txt.write(f'New Question has been asked! View question.txt!\n')
chat_txt.close()
time.sleep(2)
print("Removed from Codehelp Currently..")
else:
if owner:
with open('chat.txt', 'a') as chat_txt:
chat_txt.write(f'{crown} {name}:{message}\n')
chat_txt.close()
else:
with open('chat.txt', 'a') as chat_txt:
chat_txt.write(f'{name}: {message}\n')
chat_txt.close()



Here is the complete documentation. PEP 8 -- Style Guide for Python Code.

• I have rejected two of your previously approved suggested edits. You are not allowed to edit code in questions - 1 2. Additionally, please refrain from suggesting the same edit twice in the future. However, please feel free to suggest the text changes in this edit again if you don't edit the code. (so you get +2 rep) If you edit the code your suggestion will be rejected. Jun 25 at 19:18
• Thanks, I didn't know that and I really appreciate. Jun 26 at 0:27
• Your initial open() is redundant and can be deleted
• crown is unused because owner is hard-coded to False
• Consider using pathlib which makes several things in your code more convenient
• Your sleep calls hinder rather than enhance usability and can be deleted
• os.system('clear') is not cross-platform, and anyway it offers no usability benefit, so that can be deleted, too
• Your with / close pattern implies a double-close, which is incorrect. The point of with is that it already guarantees a close.
• Use getpass instead of input for the administrative password, to prevent over-the-shoulder

## Suggested

from getpass import getpass
from pathlib import Path

CHAT_PATH = Path('chat.txt')
QUESTION_PATH = Path('question.txt')
LOG_PATH = Path('publiclogs.txt')

print("Codehelp || Question through a terminal.")

while True:
if name != "":
break
print("Invalid: name must not be empty.")

while True:
if CHAT_PATH.exists():

message = input("Message: ")
command = message.lower()

title = input("Question title: ")
description = input("Description: ")
tags = input("Tags, separated by ',': ")

f'TITLE: {title}\n'
f'DESCRIPTION: {description}\n'
f'TAGS: {tags}\n'
f'Written by {name}\n'
f'-------------------\n'
)

with LOG_PATH.open('a') as log_txt:
log_txt.write(f'New question has been asked! View {QUESTION_PATH}\n')


• The original value of the chat variable appears to be unused - you just open the publiclogs.txt file but never use or close it
• Using open with with statements is good and is usually the way to go, but sometimes you use open on its own which just increases the risk of opening a file and forgetting to close it - you rarely want to do that
• On a related note, you don't need to manually close files if you opened them in the heading of a with statement. The ask_txt.close() and chat_txt.close() lines in the .ask part are unnecessary
• You keep re-opening "chat.txt" each pass through the loop. If you want to go back to the start of the file you usually want to use chat.seek(0) rather than re-opening the file. There are situations where closing and re-opening a file is warranted, but I'm not sure whether this is one of them
• A minor issue, but I find it annoying that both the variable chat and the variable chat_txt can sometimes refer to the file "chat.txt" and sometimes to the file "publiclogs.txt", with no obvious pattern as for which one is used when. It makes the logic harder to follow