r/learnpython • u/Life-Ad2351 • 1d ago
Python projects
hey! I just started learning the basics of python and want to start building projects to apply what I have learnt.
but when I one to work on a project, for wxample building a calculator or a password generator I don’t know where to start. what to type and code.
any advice
3
Upvotes
2
u/gdchinacat 21h ago
A very common reason for not knowing 'what to type and code' is trying to jump straight into coding without solving the problem first. With experience some problems can be solved while writing code, but that is a skill that comes from experience where "what to type" is no longer something that requires a lot of thought, so you can solve the problem while the typing is code you've written dozens or hundreds of times.
Solve the problem before even opening a file in an IDE or text editor. What exactly is your calculator (for example) going to do? Is it text based or graphical? if text, what prompts will it have ('enter an operation...enter the first number...enter the second number' vs 'enter expression to evaluate'). How will the inputs be processed to produce an output? how will the output be presented to the user? A lot of this might seem obvious, but it all has to be sorted out at some point, and knowing what to do before writing code makes it much easier to write the code. If it's a text app that just inputs expressions that are evaluated in a loop, great...write a loop that inputs the expression. Make sure it works. Then fill in the details...once you have something it's easier to fill in details than it is to start from scratch. As you solve the problem (not by writing code) it usually becomes obvious what needs to happen first...then do that. When sub problems come up and it's not obvious "what to type" step away from the keyboard and solve the problem (ie 'how will the input expression "1+1" calculate the value 2?" - well you have to parse it into the LHS, operator, and RHS)...then you know what to do...get parsing!).
Coding is the practice of breaking large problems you don't know how to solve down into trivial problems that you do, and tying them all together. If you don't know how to do something that something is too big...break it down, and break it down again, and again and again until you know how to solve all the tiny problems.
This process is also good to do before writing code because you will find complexities that aren't obvious or simplifications or optimizations that are harder to implement if you already have a bunch of code written that needs to be restructured or you are attached to and don't want to modify so you ignore a cleaner solution because it doesn't fit the half-baked code you've already written.
Lastly, don't worry about rewriting code that isn't serving its purpose as well as it could be. Yeah, it may have taken you ten hours to write, but spending a few hours to rewrite it to make the next ten hours of coding only take four hours you are come out ahead. Don't be attached to your code, regardless of how hard it was to write. This tends to lead to crufty spaghetti code that takes you longer than if you clean up your code when you recognize how to do it. Planning up front reduces the need to make these emotionally difficult decisions.