Python Forum

Full Version: a simple calculator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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("===========================================================")
you can change all those print statements (empty lines)
to:
print('\n'* 14)
Thank you ^^
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('\n'* 14)
        print("===========================================================")
        print("I                      New calculation                    I")
        print("===========================================================")
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()
looks good, have not learned how to code this way, but ill take a
look at it
This is also a great opportunity to learn about switching on dictionaries instead of writing repetitive if/elif/else blocks:
from operator import add, sub, mul, truediv


OPERATIONS = {"+" : add, "-" : sub, "*" : mul, "/" : truediv}
OPERATOR_TEXT = {"+" : "Number to add? ", "-" : "Minus which number? ",
                 "*" : "Times what? ", "/" : "Divided by which number? "}


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):
        print("\n".join(self.form['header']))

    def perform_op(self, operator):
        x = int(input("First number: "))
        y = int(input(OPERATOR_TEXT[operator]))
        return OPERATIONS[operator](x, y)
 
    def restart(self):
        print('\n' * 14)
        print("===========================================================")
        print("I                      New calculation                    I")
        print("===========================================================")
 
    def calc(self):
        self.print_header()
        while True:
            user_input = input("+,-,*,/? ")
            if user_input == "restart":
                self.restart()
            else:
                result = self.perform_op(user_input)
                print(result)
            input("results(Enter to continue)")


if __name__ == '__main__':
    Calculator()
I partially understand the code, but I could need some more help.

Also, how is it possible to let the programm itself call self defined functions?
example()
Quote:Also, how is it possible to let the programm itself call self defined functions?
I'm not sure what you mean by self defined functions.
do you mean from within the class?
I mean

def myfunc()

then you can call this function in the shell,

so if

def myfunc():
print("text")

it will print text in the shell when you type
myfunc()

can this be automated so it can be run in the terminal too?
within the class, it's called a method.
It requires the first argument to be self for example:
def myfunc(self):
    print('text')
if external from the class,
def myfunc():
    return "I'm a function"

class myClass:
    def __init__(self):
        print(myfunc())

myClass()
You can run either Merkie's code, or the code that I supplied from the command line,
just save as a .py file (say 'MyClass.py') and run as any other python script:
python MyClass.py
Pages: 1 2