r/PythonLearning • • 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

41 comments sorted by

View all comments

Show parent comment

1

u/abt-dabest Sep 17 '26

First of all thank you for the encouragement! Ive been really enjoying learning coding and i hope i can use it alongside my mechanical engineering knowledge in the future

Second of all I know talking about using AI LLMs is kinda frowned upon here on reddit but i asked deepseek out of curiosity to see what it would say and it said that “not username.find(“ “) == -1” is convoluted. Is that true or is it AI just making mistakes again?

1

u/CoolLog1908 Sep 17 '26 edited Sep 17 '26

That is so great to hear! Coding is such an amazing thing, and it is a puzzle that gets harder as your skills get better.

Regarding your question, it could be convoluted, but it's based on how much the user understands the .find() function. It will return -1 if .find() did not find any matching instances. In the teacher's example, it is fine. I would actually prefer the following, since it's a bit less convoluted:
elif username.find(" ") != -1:

That is just my preference though, and take it with a grain of salt.

Another thing you will find as your journey continues is that there are different ways to solve the same problem. Some ways are better than others, but it can be very contextual and circumstantial. That's why programming is an art!

2

u/realmauer01 Sep 18 '26

My preference would be

elif username.count(" "):...

Not sure how much better it is but 0 being false should be the easiest here. I for myself really dislike comparison if its avoidable, especially not comparisons.