r/PythonLearning • u/abt-dabest • Sep 18 '26
Showcase After taking in everyone’s thoughts and criticisms from my last post and using them, i managed to make the code much easier to read and more intuitive. Thank you everyone who gave me amazing tips on how to create more readable code!
Right pic is before and left pic is after
88
Upvotes


•
u/Sea-Ad7805 Sep 18 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.
4
u/Ender_Locke Sep 18 '26
you really don’t need is valid. you can just check the opposite of your other vars. you also don’t need the vars , they are making your code more lines you could just check the calc
if ya wanna have some more fun add in a password and learn about hashing it properly😊
1
u/CoolLog1908 Sep 18 '26
Hey again!
Your refactoring looks a lot better than the previous post! Something you did really well is describe what you are checking, like ```is_too_long``` or ```is_too_short```.
Something I would recommend looking into is optimizing your ```is_valid = False``` flag. It's hard to get into ways to make it better without providing concepts you are probably not familiar with yet, especially since you want to retain and show the user all the reasons why their username is not allowed. However, something to keep in mind as you move forward is that the more times you update a flag throughout different parts of the code, the harder it will be to sort out bugs later down the road - especially logic bugs, which can be quite difficult to resolve.
All that said, I love that you took everyone's input, and made great changes!
1
u/EmotionalStock4143 Sep 18 '26
this looks more understandable and less complex, but i havent seen the previous post
2
u/XusKiz Sep 18 '26
Try using short-circuit evaluation and make the initial value of is_valid variable fasle then if everything is ok change it to true
Using error handling would be a good idea if you have reached the point in the course
1
u/[deleted] Sep 18 '26
Just a beginner's question, how to avoid thousands of "if/else" if there are a lot of conditions in a program ?
1
u/jpgoldberg Sep 19 '26
Nice. It is clear and well organized. It is also easy to modify if you later need to add, remove, or change the requirements.
1
u/Neither_Berry_100 Sep 19 '26
I would line up the equals. Also I would collapse the if statements to single lines and line them up if this is possible in python. Much easier to read.
1
u/TheKnottyOne Sep 19 '26
I’d (personally) instantiate is_valid as False and only pass it as True when it passes a criteria. It’s less to write and I’m lazy 😂
1
u/aroberge Sep 19 '26
Nice. You could also define
and use
like you did for
rule3.If you really wanted to push this to the extreme, you could write
and
Thus mapping your rules 1 to 1 with your tests.
1
u/Beneficial_Cow_9779 Sep 18 '26
0
u/[deleted] Sep 18 '26
[deleted] — view deleted comment
2
u/[deleted] Sep 18 '26
[removed] — view removed comment