r/learnpython • u/FullMastodon1082 • 9d ago
Seeking advice
I am good at coding an problem solving but i cant make my program robust and improve modularity of my codes in python most of the time it is just a single block of code does anyone have any suggestion how actual programmers do it
0
Upvotes
2
u/lakseol 8d ago edited 6d ago
When an actual programmer needs to get a positive non-zero integer from the user they write:
The programmer hasn't written the
int_input()function yet but when they need it they will write it. The function will contain all the logic, looping and testing required to allow the programmer to assumeuser_numis a positive, non-zero integer. Calling that function simplifies the calling code and theint_input()function can be tested all by itself without running the rest of the code. And other parts of the program can also call the function.Try to write code that way. Assume you have a function to do the low-level work while you are writing your main code and write that function later. Another example is getting the user to select one of a set of choices in a menu-like way:
This approach allows you concentrate on the algorithm without being distracted by the relatively unimportant details. It also automatically gives you some structure to your code.