Python Forum
a simple calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a simple calculator
#4
Here's another way to do this:
class Calculator:
    def __init__(self):
        self.form = {
            'header': ["simple calculator by Solstice", "I-----I", "I00000I", "I-----I", "I1 2 3I", "I4 5 6I",
                       "I7 8 9I", "I-----I", "", "when the program asks for +- x : type restart to make some space"]
        }
        self.calc()

    def print_header(self):
        for line in self.form['header']:
            print(line)

    def add(self):
        x = input("first number ")
        y = input("number to add ")
        return int(x) + int(y)

    def subtract(self):
        x = input("first number ")
        y = input("minus which number? ")
        return int(x) - int(y)

    def multiply(self):
        x = input("first number ")
        y = input("times what? ")
        return int(x) * int(y)

    def divide(self):
        x = input("first number ")
        y = input("divided by which number? ")
        return int(x) / int(y)

    def restart(self):
        print('\n' * 14)
        print("===========================================================")
        print("I                      New calculation                    I")
        print("===========================================================")

    def calc(self):
        self.print_header()
        while True:
            c = input("+/-/x//? ")
            if c == "+":
                print(self.add())
            elif c == "-":
                print(self.subtract())
            elif c == "x":
                print(self.multiply())
            elif c == "//":
                print(self.divide())
            elif c == "restart":
                self.restart()
            input("results(Enter to continue)")

if __name__ == '__main__':
    Calculator()
Reply


Messages In This Thread
a simple calculator - by Solstice - Dec-29-2017, 08:04 PM
RE: a simple calculator - by Larz60+ - Dec-29-2017, 08:16 PM
RE: a simple calculator - by Solstice - Dec-29-2017, 08:25 PM
RE: a simple calculator - by Larz60+ - Dec-29-2017, 11:41 PM
RE: a simple calculator - by Solstice - Dec-29-2017, 11:59 PM
RE: a simple calculator - by Mekire - Dec-30-2017, 12:52 AM
RE: a simple calculator - by Solstice - Dec-30-2017, 01:29 PM
RE: a simple calculator - by Larz60+ - Dec-30-2017, 01:49 PM
RE: a simple calculator - by Solstice - Dec-30-2017, 02:06 PM
RE: a simple calculator - by Larz60+ - Dec-30-2017, 07:56 PM
RE: a simple calculator - by Solstice - Dec-30-2017, 10:12 PM
RE: a simple calculator - by Larz60+ - Dec-30-2017, 11:02 PM
RE: a simple calculator - by Solstice - Dec-30-2017, 11:45 PM
RE: a simple calculator - by Larz60+ - Dec-31-2017, 04:08 AM
RE: a simple calculator - by Solstice - Jan-01-2018, 09:26 PM
RE: a simple calculator - by Solstice - Jan-04-2018, 11:06 AM
RE: a simple calculator - by Solstice - Jan-10-2018, 10:31 AM
RE: a simple calculator - by Ablazesphere - Mar-10-2019, 09:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] simple calculator FelixLarry 11 4,531 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