r/learnpython • u/thedean425 • Feb 06 '26
How to actually write it
I understand python as in I know what loops are valuables input ect ect. But I don't know how to actually use it to make for example a calculator what do I do
2
u/AwkwardBugger Feb 06 '26
I imagine you want to go from writing one off scripts and functions to an actual program that runs, that takes your inputs etc, and you close it once you’re done.
I would definitely recommend being comfortable with functions and classes first if you’re not already. Then, look up some tutorials on making a program with a console based ui. Once you can handle those, you can move on to using libraries to create a gui with buttons etc.
Here’s a guide you can look at. The section “Putting It All Together: The Main Loop” shows the main program loop, which is what makes it actually work like a program rather than just a script.
1
u/thedean425 Feb 06 '26
Here I'm just coping someone else's code not actually doing somthing
2
u/AwkwardBugger Feb 06 '26
You’re allowed to read code without copying it. Look at the main program loop and try understanding it. See if you can make a ui where you just select different options and print a different string based on your choice. Ignore the to-do app parts, you could replace them with code for whatever you want to make like a calculator.
1
u/Kqyxzoj Feb 06 '26
Here’s a guide you can look at.
Random observation: that guide has "Save your tasks so they persist between sessions" as number 5 on a list of 6 items. Personally I would start with the data model and work my way from there.
1
u/AwkwardBugger Feb 06 '26
I only linked this guide because it has an example of a main program loop with a basic ui
9
u/Buttleston Feb 06 '26
Start from the smallest parts. What does your calculator need to do? Like say it is
read input from user
parse user input
perform calculation
output calculation results
Just do it one part at a time. I'd probably start with 2, and then 3, and then 4 and finally 1. My thinking here is that testing changes to code that require you to perform input is annoying and error prone because you have to re-run the program, type stuff in again, and see if it works
So start with 2. Pretend like you've already received input from the user, just set it in a variable. Start working on parsing it, etc.
5
2
u/mxldevs Feb 06 '26
You start by pulling out a piece of paper and writing out what a "calculator" is and what it's supposed to do.
Then you write out all the operations that are supported, and what components you will need in order to implement them.
Then you write code based on what you've designed.
If you can't tell me what a calculator is in complete detail, you likely won't be able to build one.
1
Feb 06 '26
This is a problem that I noticed with recent graduates. They had learned how to write an algorithm, but project structure and putting things together was not well taught.
Honestly, this is something you learn while doing. Some languages have harder rules for these things than others. Python is very loose in terms of directory trees.
So you know you're going to need a project structure:
Calculator/
src/
Calculator/
gui/
__main__.py
tests/
pyproject.tomlIn your main.py file, have a call to your gui code to init your calculator and show the window.
Tkinter or pygame or whatever your gui library is will be your next step. Do a poc to open a window, change the size, and start adding buttons.
Then you're going to want to play around with the calculator logic for the buttons. There are a bunch of tutorials for that, so you should be good to go when you research.
3
u/fakemoose Feb 06 '26
What do you mean for a calculator? Can you give more details on what you’re trying to do?