Python Forum
a simple calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a simple calculator
#11
I have tried something:
import time
run= True

def myfunc():
    a=input("first")
    b=input("second")
    c=int(a)+int(b)
    print(c)
 
class myClass:
    def __init__(self):
        print(myfunc())
        time.sleep(3)
        end=input("end?(Y/N)")
        if end=="Y":
            run= False

while run== True:
    myClass()
but it didn't really worked out
Reply
#12
With a class, you instantiate it once, and call the methods whenever needed:
import time


class myClass:
    def __init__(self):
        self.a = None
        self.b = None

    def get_a_and_b(self):
        self.a = int(input("first: "))
        self.b = int(input("second: "))

    def myfunc(self):
        c = self.a + self.b
        print(c)
        # Just to make it more
        self.a += 1
        self.b = self.a + 1
        time.sleep(3)


def main():
    # Instantiate Class
    mc = myClass()
    # Get a and b values
    mc.get_a_and_b()
    # Loop on myfunc
    while True:
        mc.myfunc()


if __name__ == '__main__':
    main()
Reply
#13
So the classes are there so you can keep it more tidy and overviewable,
right?
(I almost got it!:D)

Ok, I will probalbly need some more time untill
I fully understand the code, but somehow it will work
Reply
#14
That's part of it. Another is that if in the future you need to add capability to
a class, you can do so within the class without affecting existing software. If you
use just plain functions, you'd have to go to every place where the function was
used and accommodate for the changes. That's called extensability.

Also, a class is like a black box. a lot of what is done inside the class can be
'hidden' at least in a sense from the use of the class.

You can keep all code related to the class as methods that can be called as needed from
the outside.

For example in the code you wrote, you import the class time.
To see all of the methods that are instantaneously available to you,
just from that one import statement, do the following:
  • Start a terminal (command) window.
  • type
    python 
  • type the following:
    >>> import time
    >>> help('time')
  • explore what's in that one class.

Partial listing from help command above:
Output:
Help on built-in module time: NAME time - This module provides various functions to manipulate time values. DESCRIPTION There are two standard representations of time. One is the number of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer or a floating point number (to represent fractions of seconds). The Epoch is system-defined; on Unix, it is generally January 1st, 1970. The actual value can be retrieved by calling gmtime(0). The other representation is a tuple of 9 integers giving local time. The tuple items are: year (including century, e.g. 1998) month (1-12) day (1-31) hours (0-23) minutes (0-59) seconds (0-59) weekday (0-6, Monday is 0) Julian day (day in the year, 1-366) DST (Daylight Savings Time) flag (-1, 0 or 1) If the DST flag is 0, the time is given in the regular time zone; if it is 1, the time is given in the DST time zone; if it is -1, mktime() should guess based on the date and time. CLASSES builtins.tuple(builtins.object) struct_time class struct_time(builtins.tuple) | The time value as returned by gmtime(), localtime(), and strptime(), and | accepted by asctime(), mktime() and strftime(). May be considered as a | sequence of 9 integers. | | Note that several fields' values are not the same as those defined by | the C language standard for struct tm. For example, the value of the | field tm_year is the actual year, not year - 1900. See individual | fields' descriptions for details. | | Method resolution order:
Reply
#15
Interesting, but i have some real live problems that keep me down.
I'll look how much I can learn in the upcoming time.
Reply
#16
I just had an Idea.

Instead of classes(The stuff that I totally dont get)

You also could use definitions, comments, right?

like this:

#=====================
#content name #
#=====================
content
#=====================
Reply
#17
I think i finally understand how to make it work.
does this code look good to you?

class calculator:
    def __intit__(self):
        self.num1=None
        self.num2=None
        self.num3=None
        self.mode="none"

    def banner(self):
        print("░▒▓██████████████████▓▒░")
        print("░Calculator by solstice░")# bug in the site, not my fault
        print("░▒▓██████████████████▓▒░")

    def choose_mode(self):
        self.mode=input("+,-,*,/?")

    def get_numbers(self):
        self.num1=input("input number one")
        self.num2=input("input number two")

    def calculate(self):
        if self.mode == "+":
            self.num3=int(self.num1)+int(self.num2)

        if self.mode == "-":
            self.num3=int(self.num1)-int(self.num2)

        if self.mode == "*":
            self.num3=int(self.num1)*int(self.num2)

        if self.mode == "/":
            self.num3=int(self.num1)/int(self.num2)

        print(self.num3)

def main():
    c = calculator()
    c.__init__()
    c.banner()
    c.choose_mode()
    c.get_numbers()
    c.calculate()

while True:
    main()
Reply
#18
(Dec-29-2017, 08:04 PM)Solstice Wrote: A simple calculator, for simple calculations.
print("simple calculator by Solstice")
print("I-----I")
print("I00000I")
print("I-----I")
print("I1 2 3I")
print("I4 5 6I")
print("I7 8 9I")
print("I-----I")
print("        ")
print("when the program asks for +- x : type restart to make some space")
f=1
while f==1:
    c=input("+/-/x/:?")
    if c=="+":
        x=input("first number ")
        y=input("number to add ")
        z=int(x)+int(y)
        print(z)
        input("results(Enter to continue)")
    if c=="-":
        x=input("first number ")
        y=input("minus which number? ")
        z=int(x)-int(y)
        print(z)
        input("results(Enter to continue)")
    if c=="x":
        x=input("first number ")
        y=input("times what? ")
        z=int(x)*int(y)
        print(z)
        input("results(Enter to continue)")
    if c==":":
        x=input("first number ")
        y=input("divided by which number? ")
        z=int(x)/int(y)
        print(z)
        input("results(Enter to continue)")
    if c=="restart":
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print("===========================================================")
        print("I                      New calculation                    I")
        print("===========================================================")

Nice program but i would have coded in just a single line.
user = eval(input("Enter a number : "))  #for eg "2/3" or "2*3" or "2+2"
print(user)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] simple calculator FelixLarry 11 4,200 Aug-20-2022, 11:34 AM
Last Post: FelixLarry

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020