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.

1

u/caifaisai Jul 09 '20

That was helpful in trying to get a handle on the usefulness of classes and objects, thanks. One question I have (well kinda more like 2).

When you call print(new), does the instance of the Calculation class, new, know that the print function is supposed to access the method defined in __ str __? I suppose I mean how come that method doesn't seem to be accessed with a dot followed by method name like in new.add()?

Is there something particular about the double underscores (and what do they signify? I see them alot but not exactly sure what their purpose is). Or is it something to do with that the __ str __ method seems to be the only one with an explicitly defined return value? Or something else entirely? Hopefully I phrased my question well enough, but I'm still not very knowledgeable on OOP and related terminology.

3

u/heyimpumpkin Jul 09 '20

does the instance of the Calculation class, new, know that the print function is supposed to access the method defined in __ str __?

yes

I suppose I mean how come that method doesn't seem to be accessed with a dot followed by method name like in new.add()?

that's because it's double underscore method

We can retrieve same information through new.curValue. But if our objects contains multiple parameters, using __ str__ we may specify which ones we want to show.

Double underscores is a little advanced topic, python under the hoods. There are plenty of things besides str_. for example, if you want to add one object instance to another, it wouldn't know how to do it. But you can define it useing add .

You can read the docs about all those things here https://docs.python.org/3/reference/datamodel.html