a simple calculator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: a simple calculator (/thread-7236.html) Pages:
1
2
|
a simple calculator - Solstice - Dec-29-2017 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("===========================================================") RE: a simple calculator - Larz60+ - Dec-29-2017 you can change all those print statements (empty lines) to: print('\n'* 14) RE: a simple calculator - Solstice - Dec-29-2017 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("===========================================================") RE: a simple calculator - Larz60+ - Dec-29-2017 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() RE: a simple calculator - Solstice - Dec-29-2017 looks good, have not learned how to code this way, but ill take a look at it RE: a simple calculator - Mekire - Dec-30-2017 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() RE: a simple calculator - Solstice - Dec-30-2017 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() RE: a simple calculator - Larz60+ - Dec-30-2017 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? RE: a simple calculator - Solstice - Dec-30-2017 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? RE: a simple calculator - Larz60+ - Dec-30-2017 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 |