r/learnpython • u/mexicanlefty • Jul 09 '20
I made a calculator in Python!
Hello everyone!
I just wanted to share this project i made, its not my first programming project or python project whatsoever, but it is indeed the first one i didnt depended much on google or a video tutorial so that for me its a great advance.
The project is also my first on a github repository!
https://github.com/mexicanlefty/Python-Calculator
Please take a look and test it, in case you can provide any feedback it would be even better, it doesnt matter if you guys want to be very critical, i dont mind i want to keep practicing and i already have my next project in mind.
8
u/njape Jul 09 '20
Overall I think it looks good, nice work!
Small comment is that I would change the mode numbers to constants or similar for better readability.
As already noted I don't like global variables, it is a sin. For future projects, and maybe also as an addition to this project, I would urge you to add some unit tests to the code. While doing this you will notice one of the reasons global variables should be avoided. It will also help you to improve how you go about designing your program so that it is modular and testable. Don't care about 100%test coverage in the tests, focus on what functionality is important, special cases and edge cases and you should find a good middle ground.
If you find it hard to isolate a function for unit testing, without major mocking, you most likely have some problem with the design.
Keep up the good work!
2
u/mexicanlefty Jul 09 '20
Yeah, you are right i still have issues with code design. Any advice on how can i improve it?
2
u/njape Jul 09 '20
First a note I prefer to write the backend code, and does not have much experience with gui programming, or tkinter.
From a testing perspective I'm guessing your main issues comes from the global variables that are used within each function you would like to create tests for. There are a couple of possible solutions to this, s I me examples.
Wrapping the calculator and its functions in a class which will contain your global variables. This would allow you to prep a calculator object in different ways before running each callback function.
Change so that the callback function does not use the global variables, but instead take the necessary values as inputs. This will make the testing setup trivial, but it will require you to define function stubs, or lambda functions calling the correct function with the global variables.
8
u/clae_machinegun Jul 09 '20
First, congratulations on your first project!
I found that it can only use integer base or power in Xy function.
If you try 7 ** 0.5 it breaks.
4
u/mexicanlefty Jul 09 '20
Heyy thanks for that!
Didnt notice that didnt work, i will fix it right away.
5
u/d02d33pak Jul 09 '20
I made something similar (but basic) using PyQt5, here's the GitHub link. I would like to get some feedback too.
1
u/mexicanlefty Jul 09 '20
Hey your apps look nice, more pretty that tkinter, honestly i dont think im experienced enough to give you feedback, im rather amused by the notepad app how did you do it?
2
u/d02d33pak Jul 09 '20
All the code is in the repo itself. Check it out, star 🌟 it, fork 🍽 it, learn, improve it, do whatever. 👍
3
u/TheDankestPutin Jul 09 '20
Personally i dont like the use of global, you could try and make it OO. But the code does look fine overall.
1
u/mexicanlefty Jul 09 '20 edited Jul 09 '20
I thought about that, but i dont know why it seemed easier this way for me.
3
u/ilikewatermelonss Jul 09 '20
It's a very nice program, I wish my first project looked like this. Just a note though, you used 200+ lines of code for something that could be coded in 100 or so. This isn't anything serious, but try to keep everything simple. I will make sure to follow you on Github to see more interesting projects like this one.
1
u/mexicanlefty Jul 09 '20
Thank you, i know its not perfect and it can be simplified, i have a codewars account where i do coding challenges and usually my answer is longer than most.
3
u/toobrokeforboba Jul 09 '20
You might want to avoid wildcard import like from tkinter import * and only import the specific functions or variables you need as you code along. If that is too cumbersome, just import the whole module or you can do import tkinter as tk. The reason is if someone like me reading your code, I can’t tell immediate if you’re calling a tkinter function or your function.
1
2
u/G-Fieri Jul 09 '20
I'm thinking about doing this. Do you feel like typing this out improved they way you think algebraically at all? Also a cool thing you might want to do is a graphing calculator!
3
u/mexicanlefty Jul 09 '20
I think it is a very cool beginner project, i decided to tackle it since mathemathics have always been something im good at, in school my favorite subjects were math, geography, history and PE (weird combination i know) thats why i decided to tackle this. Although i can say, learning to code had made me relearn a lot of math stuff i didnt use since high school.
2
u/G-Fieri Jul 09 '20
Yeah I was busy being stupid in school lol programming is reigniting my interest in math because traditional studying is kind of boring for me. It makes more sense to me when I can apply it
1
u/mexicanlefty Jul 09 '20
Yeah i guess the short answer to your original question is "yes" i think the big challenge with the calculator was the string to int or float manipulation.
But i get your point, like always on high school the question asked was: how can i use this in real life? I always thought well with this i can pass the course or subject, the problem with that is once done you forget about it.
2
2
Jul 09 '20
Wow, this is so beautiful, I really want to make a calculator in python too, can you please suggest things I can look at to make something like that? like learning tkinter?
1
u/mexicanlefty Jul 09 '20
Well i can recommend freecodecamp on youtube, they have a great tkinter tutorial video, i didnt watch all, just learned the basic stuff to make my calculator, you can go from there buddy.
2
u/raddus007 Jul 10 '20
You have done great. I think everyone create calculator when they start programming so, you need to create project based on reak life problems and I promise you that tt will be very helpful to understand the concept of programming. Again, your Project is amazing but we need to go ahead so far. 👍👍
1
2
u/Jhchimaira14 Jul 16 '20
Awesome. Hey, if you remake it in MarvelSandbox, you can add it to the App Samples. It's easier to use than tkinter and looks better! Plus we could use the testing!
1
10
u/james_fryer Jul 09 '20
Let's say I want to add 7 + 2.
On my pocket calculator I do this:
a. Press 7 (7 is displayed)
b. Press + (7 is still displayed, also a small + icon)
c. Press 2 (7 disappears, 2 is now displayed, along with the + icon)
d. Press = (9 is displayed)
Now I have a choice, either I can use the result (9) of the previous sum by pressing an operator, in which case it will behave as (b) above. Or, I can start a new sum by entering a number, in which case the current display is replaced with the new number as at (a) above.
On your calculator, at (b) the display is cleared, which is strange. And after (d) if I enter a new number, it is appended to the current display.
So I'd say you need to study the behaviour of a pocket calculator and make sure your program emulates it accurately.
Note that at (b) the operator is displayed, this is a feature of my calculator, but I like it.
Most calculators also support a "Constant" function. E.g. if you press 2, then '+' twice, then a number, each press of = will add 2 to the result. This is indicated by a "K" icon. Once you have the normal behaviour correct it would be interesting to add this feature.
1
u/Chinedu_notlis Jul 09 '20
Love the username
2
u/mexicanlefty Jul 09 '20
Thanks! Well basically im mexican and left handed, thats why, been using it as my steam gamertag for a while.
1
1
u/912827161 Jul 09 '20
on a scale of 1 to 10 how difficult is it to code a calculator?
1
u/mexicanlefty Jul 09 '20
Well i consider myself a beginner, it took me like 2 or 3 hours per day (it took me like 3 days ).
I would say for me it was like a 3-4 out of 10 considering ive been programming for 8 months and still my code has bugs to fix and could have a better design.
2
2
u/raddus007 Jul 10 '20
You have done great. I think everyone create calculator when they start programming so, you need to create project based on reak life problems and I promise you that tt will be very helpful to understand the concept of programming. Again, your Project is amazing but we need to go ahead so far. 👍👍
2
Jul 09 '20
[removed] — view removed comment
1
u/mexicanlefty Jul 09 '20
Cualquier ayuda se agradece :). Yo también sigo el curso de ATBS, pero aún no lo termino y sigo desafíos en codewars, pero me falta para la POO.
2
80
u/heyimpumpkin Jul 09 '20
divison by 0 breaks your calculator, most actions aren't performed after it and it doesn't display anyhting to user
entry on button_click is not used.
since empty string evaluates to False in python, you don't need to add != "" part, you can just leave it as
also, I didn't get into it too much but I don't understand why you need spearate current value variable for each operation?
overall it's cool you made it and it works.
A better way to structure it would be using classes. You wouldn't need globals and code would be better structured and less verbose overall.