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.

11

u/[deleted] Jul 09 '20

I'm in lesson 37 of ATBSWP, but he didnt talk about classes

Could you give me a brief explanation

37

u/heyimpumpkin Jul 09 '20 edited Jul 09 '20

instead of using a separate variable and function for everything, that are unrelated, you create a class that encapsulates some type of abstract object. The object can contain those variables and functions only this object can work with(called methods).

example, you create class calculation which can hold current variable, and has methods of adding, multiplying etc. They can be then applied to any instance of the object in a pretty straightforward way.

simple example:

class Calculation:
    def __init__(self, curValue):
        self.curValue = curValue

#Calculation object will hold a value which is definied in __init__. 
#Self is referencing an instance of object.

    def add(self, operationValue):
        self.curValue += operationValue
        #add is a Calculatuion method which accesses object's 
     current value, and takes in a new value to add to it

    # this is how object will be represented once you call it 
    def __str__(self):
        return str(self.curValue)

#creating instance of our object under variable new, passing value 5 to it
new = Calculation(5)

#adding 5 to it using our add method.
new.add(5)

#this will output 10
print(new)

The beauty of it is once you wrote a class you can reuse this code, make operations with instances of class, get its parameters in very little amount of code without turning everything into mess, basically everything will come down to:

instance = Object(required parameters)

instance.method(required input)

instance.parameter

you can create as many object instances as you want, they can hold any amount of info and methods you want.

you can call Calculation.curValue every time you need to get this info(or any other containing in __init__ of you object).

Python modules you work with are build in same way. When you import random lets say, you use class 'random', when you call random.randint you use it's method randint, and when you specify value like random.randint(1, 8) you're passing parameters to its intsance/method.

6

u/mexicanlefty Jul 09 '20

Thanks, you made it very clear.