r/pythontips • u/[deleted] • Jan 27 '22
Syntax First program with 170 lines of code.
This is the first time I wrote a "big" program. I began to learn python on 'n off about 2 weeks ago. What should/could I improve in my "writing".
>Code
12
u/ziaaron Jan 27 '22
If have a few suggestions:
use
if tuitinstead ofif tuit == Truedrop off the
passstatements if you have some code in anifblockuse the
elsestatement if you two complementary conditionsuse consistent formatting (e.g. the same number of newlines after each method)
avoid using the
globalkeyworddon't use hardcoded values in multiple places (like
175in your code, and use something likeBLACK = "black"at the beginning of the file or better use Enums)in an
try/exceptblock, be specific about the exceptionsdon't use variable names in german (logindaten), just stick with english
use self explaining variable names
For your first two weeks that's pretty good, just keep learning.
2
3
Jan 27 '22
Nice! Few comments:
better file name
put all the functions into a class
make a new main.py file that instantiates and runs the class
4
u/FancyASlurpie Jan 28 '22 edited Jan 29 '22
Use a logger, and then you won't need to leave commented out print statements (you can just adjust the log level to hide/show them as necessary).
Don't use pass inside except blocks like that, now its invisible as to something went wrong.
Use PEP8 consistent naming of functions (maybe in general just run this code through a library like Black
Avoid using globals, and instead pass the variables into the functions that need them, or create a class to hold the state.
The code around if uit is false/true, could be rewritten like this:
becomes:
I suspect the code in the password check which has a similar loop could be rewritten as well.
Rename the file to have a .py ending, that way you will get syntax highlighting on github
If you create a function you can reduce the repeated code in blocks like this:
could be written as:
This is useful as now you only need to edit a single place if you want to change the colour of your fields, or the width of borders etc. Making it easier to edit + maintain your code. If you end up needing more variety in terms of the Tk.Entrys you could pass in an object which contains the settings you want it to create (rather than having a very long list of parameters on the function).
Overall really good start, keep at it and take the time to look back at your code and think how you can improve it in terms of readability, and maintainability.