Dec-25-2017, 01:15 AM
I'm having difficulty grasping the idea behind oop. I'm at a stage where I can write working programs but when I separate states and activities into separate modules I get tripped up. I took the simplest program (that requires some user interactivity) I could think of. Extremely easy program:
#bottles0.py bottles = int(input('How many bottles? ')) while True: print('bottle count: ', bottles) bottles -=1 if bottles > 1: print('there are ', bottles ,' left.') elif bottles == 1: print('There is only ',bottles ,' left.') else: print('There are no more bottles left.') break print('Happy Holidays!')Now when I get into "proper" oop:
#bottleclass.py class Bottles(): def __init__(self): self.llist = [] def many(self, num): print(num ,'bottles on the wall.') self.llist.append(num) num -=1 return num def lower(self): for ll in self.llist.reverse(): if ll == '1': print('there\'s', ll, 'bottle on the wall.') elif ll == '0': print('there are no more bottles on the wall.') else: print('there are ',ll, 'bottles on the wall')
#bottles0run.py from bottleclass import Bottles B = Bottles() while True: n = int(input('Starting bottles: ')) if n == '0' or n =='': break else: B.many(n) B.lower()So it is an unmitigated disaster. How would I get this simple program in oop? What exactly is the advantage to oop style? - What will be the advantage when I get to the stage where I can make sense of it?