So recently I learned python or like the basics of it at least and I coded a very basic project. Whenever an employee input their code the program writes a log in a .txt file. Right now its very basic I want to add GUI or UI and improve it. Heres the code:https://github.com/DUnfel/Employee-Checker
#π Feedback on my first so called "project"
12 messages Β· Page 1 of 1 (latest)
@minor sandal
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
- Adding a proper README is always a nice option. Your program is quite simple, so it's not necessary, but showing how the code is used will eventually become useful as you do larger projects.
- Use
withblocks to make sure that files get closed. If you manually callclose, that call can be skipped if you forget it in one case, or of an exception happens and causes that call to be skipped. - Variables names should be in lower_snake_case, even if they're representing names.
- Instead of using separate variables for the names, I'd use a dictionary.
- All those
ifbranches are nearly identical. As a challenge, try combining them by noting what's common and different between them.
- Im not sure how to use "with" blocks
- wdym dictionary. Like a list?
- I dont understand what do you mean on the last one.
English isnt my first language. Sry
Thanks for help π
1,
with open("Employees.txt", "a") as employe_file:
employe_file.write("\nDan was here on ")
employe_file.write(time.strftime("%b-%d-%Y %H:%M:%S"))
Now you don't need to close() employe_file
It will be automatically closed after Python finishes everything inside the with
Look into a Real Python page on the topic (I'm assuming one exists) or the standard docs. Essentially, they automatically do something when the block is exited. For file, they ensure files are always closed.
Dictionaries are a fundamental structure, like lists. If you haven't learned them yet, you should do that soon. They are very important to understand.
And for the last point, all of the if bodies look nearly identical. You could try to combine them so you aren't duplicating code.
2,
employees = {
"1237": "Dan"
"1235": "John",
"1236": "Jeniffer",
"1238": "Mike",
}
Now you can do employee_name = employees[code] instead of having to type in the name each time
For the last point, think about it like this: what happens if the company grows and you get 100+ employees? Are you going to copy and paste a new if to do file writes every time a new employee joins the company?
And what heppens if you ever want to change what data gets written? If you have a separate if check for every employee, you'd need to go to every .write call and make duplicate changes in every case. That does not scale well.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.