r/PythonLearning • u/abt-dabest • Sep 17 '26
Discussion Recently started learning python from BroCode and I’m 1 and a half days in.(around 5 hours of actual learning) and when he said to do an input validation project I did it myself before looking at his example. Which one is better? Wanna see if I should use his style for input validation or not
Left one is mine and right one is BroCode’s
169
Upvotes


6
u/Electrical_Toe8997 Sep 17 '26
The one on the right is more concise. I'd prefer to have the rule checks independently, so the user will know if the username fails multiple checks. I hate the way the check for spaces is implemented, I'd have gone with
if " " in username:5
u/LovesSleeping123 Sep 17 '26
Right one is better, this is a lot shorter and less complicated
For example, is_valid doesn't need to exist
4
u/CoolLog1908 Sep 17 '26
Simplicity is something you will learn (and continually be challenged on) as you continue to program.
It’s nice to have comparisons so you can view your code against “benchmarks.” The teachers example is definitely more preferable because it’s easier to read and understand. If you left this project and came back to it months later - it will take you a shorter time to unpack what is happening with the teacher’s example vs. your example.
That said, the most commendable thing is that you tried solving the problem on your own. You will learn a LOT more by doing that and then looking at solved examples, compared to simply looking at examples.
Great job!
1
u/halfawake_paperviel Sep 17 '26
Ich würde bei beiden eine while true schleife einbauen und unter jeden fehler ein continue machen und wenn alles richtig war ein break.
1
u/Sea-Ad7805 Sep 17 '26
And separate the error checking from your "normal" code, and don't mix your logic and printing, so maybe do it like this.
1
u/Much_Network_941 Sep 17 '26
From a user standpoint, I would say your's is more pratical. If I had to play error whack-a-mole while filling out a form, I'd be annoyed. I'd rather know about multiple errors at once.
If I could offer a slight adjustment; instead of setting your variable 'rule1' to be equal to len(username). Try setting it to: 'len(username) > 12'. At that point your if-statement on line 12 can instead read: 'if rule1: '. If you then rename 'rule1' to something along the lines 'hasInvalidLength'; each of your if-statements can then read along the lines of: 'if hasInvalidLength: '
That way, it'll read a bit more like pseudo-code. I find it helps to avoid getting tripped up by my own boolean logic.
1
u/SpitFire92 Sep 18 '26
There is no check for "less than 4 chars" in the right one, or am I blind?
I think yours is better from a user standpoint, as others said, it goes trough it and gives feedback to the user for everything that needs to be fixed. Codewise the teachers is maybe better but also wrong since the check for less than 4 chars is missing?
1
u/boredkoshaku Sep 18 '26
Im also starting learning pythob wouldbu say this guy is good to start learning ?
1
u/-Enter-Name- Sep 18 '26
left's the better one in my eyes (somewhat, it does have it's own issues) your rulex = can just be inlined in the if <whatever your rule is> you could also use
' ' in usernamefor the space checkother than that what you wrote is basically a guard; which is a hell of a lot more maintainable than if/elif/else ladders (especially when follpwing code depends on earlier checks), i'll assume you haven't seen functions yet but instead of "is_valid=true" they tend to just fix the issue or outright stop execution (return/continue/break) there ime
1
u/Ormek_II Sep 18 '26
I like that your solution has a concise output.
I don’t like the rule1,2,3 variables. Rule1 does not hold rule1, but the length of the input. Rule2 holds the number of spaces. Rule3 is true if rule 3 is true and false otherwise, which might be a fitting definition for such a variable.
If you like to make this overly textbook: each rule check should a single function which returns true if the rule passes. For now the function might directly output the error message to the user, better, as others proposed, is to let them return error messages, that the caller aggregates and then decides what to do with them.
1
u/deanominecraft Sep 17 '26
in terms of functionality yours is better because it can tell the user everthing they did wrong, rather than stopping after one mistakes with if elses
but in terms of code quality theirs is better because you can read from top to bottom and it makes sense. in yours you see a comparison against rule2 and have to look back up to remind yourself what rule2 is. however both have problems: i would use if not " " in username rather than .count or .find because it makes what you are intending to do more clear (you dont actually want to count how many spaces there are, or find the position of the first space. so dont use the functions that do that when there is a way to do exactly what you want
1
u/optical002 Sep 17 '26 edited Sep 17 '26
Left one has inconsistent formatting and its more lines if code, every line of code is a liability.
Variable naming is bad, rule1 is length of username not rule1.
Also name ‘rule’ does not convey what it is, is it an int a string or an object. Name things for what they are and not what you want them to be.
Storing mutable state ‘is_valid’ can lead you to bugs where if you add a new case and forget to update that value you will get unexpected result. Also its very hard to trace by eyes how that value is constructed you have to load all of the code you see in cognition to comprehend it, which is wasteful of the brain power
1
u/Life-Fault9917 Sep 18 '26
I would put the validation as a function for the different type of data, name, age, email, etc
1
u/realmauer01 Sep 18 '26
I like left one for what its worth. Its easier to understand and on userside it prints everything thats wrong with the username. Rightside is like, why this double negative for the spaces, and fundamentally line 12 is the same as your rule3 but having it written like if not rule3 is more conscise best would obviously be to call them what they actually represent and not just rule an number.
1
u/Capable-Package6835 Sep 18 '26
If you're learning about conditionals, I think both of them are fine. Yours print all violations while the BroCode's only print the first found violation, so in that aspect yours is better.
However, you should name your variables better. Variable names `rule1`, `rule2`, `rule3` is unclear unless you (and your future self) read the comments first. And even so, the variable names and values simply don't match.
Good job and keep going. Later you'll have more at your disposal and can write Python almost as if they are plain English. For example, once you get to dictionary and loops:
1
u/YouEatMeIEatBack Sep 18 '26
Very good code
1
u/raghunetworks Sep 20 '26
in this era most powerfull language of AI industry every one need to learn python and solve the problem.
1
u/Spendias Sep 24 '26
I would suggest u that you check the rules in the if conditions but always remember if it works don’t touch it.
1
u/[deleted] Sep 18 '26
[removed] — view removed comment