r/learnpython • • 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.

392 Upvotes

49 comments sorted by

View all comments

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.

if current_number_minus != "":

since empty string evaluates to False in python, you don't need to add != "" part, you can just leave it as

if current_number_minus:

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.

3

u/mexicanlefty Jul 09 '20

Thanks i would look into that, i kinda overlooked the division by 0 but will fix it. The reason i used different current value for each operation was that (on my mind) it seemed easier since i was using global variables (i still find it hard to use objects and classes, though im working on that)

3

u/1Tim1_15 Jul 09 '20

Using classes (object oriented programming) is good in some cases but not in others. When it doesn't provide a big benefit (and measuring the benefit(s) is subjective), I opt for not using classes. In your case, I could see going either way.

There are good reasons why many beginner Python books don't go into classes, or only cover them minimally. They should be learned eventually, but for simpler projects I rarely use them.

For a good explanation of why using classes isn't always a good idea, see what this Python core developer had to say: https://www.youtube.com/watch?v=o9pEzgHorH0&list

Just to emphasize: I'm not saying classes shouldn't be used. They are very helpful in some cases. But not using them is fine in many cases - it just depends.