| | |
Is this an OO program?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Okay, I wrote a simple calculator, not in GUI form, but a calculator for simple stuff. So I'm wondering, would this layout be considered OO? I think this is how it works. Now I do know that I didn't use an object to handle the class since it just wasn't necessary. Now I know that making a class wasn't totally necessary for this program, but I'm just curious if this would be the general layout. So here's the code:
Python Syntax (Toggle Plain Text)
import os import math import fractions if os.name == "nt": os.system("color 48") os.system("title Calculator") def clear(): os.system("cls") else: def clear(): os.system("clear") class Calc: def menu(): clear() print ("Welcome to calculator!") print ("----------------------") print ("1) Exponents ") print ("2) Division ") print ("3) Multiplication ") print ("4) Addition ") print ("5) Subtraction ") print ("6) Square Root ") print ("7) Simplify Fractions ") print ("8) Factors ") print ("9) Exit ") print ("----------------------") menu = str(input("Choose one: ")) if ( menu in list("123456789") ) == False: clear() print ("Please type a valid option!") input () Calc.menu() methods[int(menu)-1]() def exp(): clear() print ("? to the - power equals -") p1 = int(input()) print () print (p1, "to the ? power equals -") p2 = int(input ()) print () p3 = pow(p1, p2) print (p1, "to the", p2, "power equals", p3) input () Calc.menu() def div(): clear() print ("? divided by - equals -") d1 = int(input()) print () print (d1, "divided by ? equals -") d2 = int(input()) print () d3 = d1/d2 if (d1 % d2) == 0: d3 = int(d3) print (d1, "divided by", d2, "equals", d3) input () Calc.menu() def times(): clear() print ("? multiplied by - equals -") m1 = int(input()) print () print (m1, "multiplied by ? equals -") m2 = int(input()) print () m3 = m1*m2 print (m1, "multiplied by", m2, "equals", m3) input () Calc.menu() def add(): clear() print ("? plus - equals -") a1 = int(input()) print () print (a1, "plus ? equals -") a2 = int(input()) print () a3 = a1+a2 print (a1, "plus", a2, "equals" ,a3) input () Calc.menu() def minus(): clear() print ("? minus - equals -") s1 = int(input()) print () print (s1, "minus ? equals -") s2 = int(input()) print () s3 = s1-s2 print (s1, "minus", s2, "equals", s3) input () Calc.menu() def square(): clear() print ("The Square Root of ? is -") sqrt1 = int(input()) sqrt2 = math.sqrt(sqrt1) if ( str(sqrt2).split(".")[1] ) == "0": sqrt2 = int(sqrt2) print () print ("The Square Root of", sqrt1, "is", sqrt2) input () Calc.menu() def frac(): clear() print ("?/- simplified is -/-") fr1 = int(input()) fr11 = str(fr1) print () print (fr11 + "/- simplified is -/-") fr2 = int(input()) fr22 = str(fr2) print () fr3 = fractions.Fraction(fr1, fr2) fr33 = str(fr3) print (fr11 + "/" + fr22, "simplified is", fr33) input () Calc.menu() def factors(): clear() f = int(input("Type the number you would like to find the factors of: ")) f2 = [] for f1 in range(f + 1): if f1 != 0: if (f % f1) == 0: f2.append(str(f1)) print ("The factors of", f, "are", ",".join(f2)) input () Calc.menu() methods = [ Calc.exp, Calc.div, Calc.times, Calc.add, Calc.minus, Calc.square, Calc.frac, Calc.factors ] Calc.menu()
Last edited by AutoPython; Oct 25th, 2009 at 12:06 am.
0
#3 Oct 25th, 2009
Well, it's bad OOP.
Not that I actually think that everything has to be OOP mind you (some small programs are just better off without it), but take for example your methods list. Why is your class directly manipulating an external object which it does not own (or was not given)?
What does displaying a menu have to do with a calculator? I don't necessarily agree that you need a class for something so small though. Perhaps a function that returns the index of the function selected, or better yet the name (so you can just do
Not that I actually think that everything has to be OOP mind you (some small programs are just better off without it), but take for example your methods list. Why is your class directly manipulating an external object which it does not own (or was not given)?
What does displaying a menu have to do with a calculator? I don't necessarily agree that you need a class for something so small though. Perhaps a function that returns the index of the function selected, or better yet the name (so you can just do
getattr(c, name)() ) Last edited by scru; Oct 25th, 2009 at 10:30 am.
0
#5 Oct 25th, 2009
•
•
•
•
(some small programs are just better off without it)
•
•
•
•
Why is your class directly manipulating an external object which it does not own (or was not given)?
•
•
•
•
What does displaying a menu have to do with a calculator?
Last edited by AutoPython; Oct 25th, 2009 at 11:15 am.
0
#7 Oct 25th, 2009
•
•
•
•
What you have in your class right now are functions, not methods. You can turn your functions into methods with 'self'. This way you can also avoid globals outside the class, like your method list.
self.methodname() (note that you don't put self back in, when calling methods, python automatically sends in the object instance, ergo self, as the first argument.) •
•
Join Date: Aug 2008
Posts: 160
Reputation:
Solved Threads: 48
-1
#9 Oct 25th, 2009
This code had been better without a class,and this is not the OOP way of thinking.
One function for Addition/Division....... can be shorten by eval()
Here an example with class.
One function for Addition/Division....... can be shorten by eval()
Python Syntax (Toggle Plain Text)
>>> a = eval('5+8886/45*4554545') >>> a 899370824.3333334
Here an example with class.
Python Syntax (Toggle Plain Text)
import math class Calculator(object): '''Doc_string info about class''' def calculate(self, expression): self.value = eval(expression) def square(self, sqrt1): '''This look like a function,but inside a class is called a method''' self.b = math.sqrt(sqrt1) def __str__(self): return self.value return self.b #here a make the method of class short #and i do user_input and printing outside of the class(you can of course make a menu but not in the class) a = Calculator() print('Calculate +-*/ and Square Root') b = input('values') a.calculate(b) print('The sum of %s is %d' % (b, a.value)) c = int(input('Enter Square Root value')) a.square(c) print('The square Root of %d is %d' % (c, a.b)) ''' my output--> Calculate +-*/ and Square Root The sum of 45+78*4545/56 is 6375 The square Root of 25 is 5 '''
Last edited by snippsat; Oct 25th, 2009 at 5:47 pm.
![]() |
Similar Threads
- AutoExecuting a program Without Explorer (Windows NT / 2000 / XP)
- Program help required (Assembly)
- Help with a program (Java)
- Add compression to my program (C++)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- 3d Program (Game Development)
Other Threads in the Python Forum
- Previous Thread: Check for Excel session
- Next Thread: Making an official-looking window
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change command convert count csv cturtle cursor def dictionary digital dynamic dynamically enter event examples external file float format frange function google gui hints homework i/o iframe import info input jaunty java keyboard lapse line linux list lists loop microphone mouse multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pygtk pyopengl python random recursion return scrolledtext signal skinning sprite stderr string strings subprocess table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable voip web-scrape whileloop windows wxpython






