![]() |
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
|
RE: a simple calculator - Solstice - Dec-30-2017 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 RE: a simple calculator - Larz60+ - Dec-30-2017 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() RE: a simple calculator - Solstice - Dec-30-2017 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 RE: a simple calculator - Larz60+ - Dec-31-2017 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:
Partial listing from help command above:
RE: a simple calculator - Solstice - Jan-01-2018 Interesting, but i have some real live problems that keep me down. I'll look how much I can learn in the upcoming time. RE: a simple calculator - Solstice - Jan-04-2018 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 #===================== RE: a simple calculator - Solstice - Jan-10-2018 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() RE: a simple calculator - Ablazesphere - Mar-10-2019 (Dec-29-2017, 08:04 PM)Solstice Wrote: A simple calculator, for simple calculations. 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) |