summaryrefslogtreecommitdiff
path: root/sample_notebooks/Aman KumarJain/Chapter_6_Objects_and_Classes_1.ipynb
blob: 89a18f9934008700ab06a9be75f3f8f7f21fc432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
{
 "metadata": {
  "name": "",
  "signature": "sha256:bd4e7931ddf89d8dc8befb681e1e57c7a9c742cd8abe8e18a494a55156d56cee"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 6: Objects and Classes"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.1, Page Number: 216"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class smallobj:    #define a class\n",
      "    \n",
      "    def setdata(self,d):    #member function to set class variable somdata\n",
      "        self.__somedata = d\n",
      "        \n",
      "    def showdata(self):     #member function to display somedata \n",
      "        print 'Data is ' , self.__somedata\n",
      "\n",
      "\n",
      "#define two objects of class smallobj\n",
      "s1=smallobj()\n",
      "s2=smallobj()\n",
      "\n",
      "#call member function to set data \n",
      "s1.setdata(1066)\n",
      "s2.setdata(1776)\n",
      "\n",
      "#call member function to display data \n",
      "s1.showdata()\n",
      "s2.showdata()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Data is  1066\n",
        "Data is  1776\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.2, Page Number: 223"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class part:    #define class \n",
      "    \n",
      "    def setpart(self,mn,pn,c):    #set data\n",
      "        self.__modelnumber = mn\n",
      "        self.__partnumber = pn\n",
      "        self.__cost = c\n",
      "    \n",
      "    def showpart(self):          #display data \n",
      "        print 'Model' , self.__modelnumber ,\n",
      "        print ', part' , self.__partnumber , \n",
      "        print ', costs $',self.__cost\n",
      "        \n",
      "#define object of class part     \n",
      "part1 = part()\n",
      "\n",
      "#call member function setpart\n",
      "part1.setpart(6244,373,217.55)\n",
      "\n",
      "#call member function showpart\n",
      "part1.showpart()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Model 6244 , part 373 , costs $ 217.55\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.3, Page Number: 225"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from turtle import *     #importing turtles library\n",
      "\n",
      "class circle:            #defining circle class\n",
      "    \n",
      "    def set(self,x,y,r,fc):    #sets circle attribute\n",
      "        self._xCo = x\n",
      "        self._yCo = y\n",
      "        self._radius = r\n",
      "        self._fillcolor = fc\n",
      "        \n",
      "    def draw(self):         #draws the circle \n",
      "        setup()             #set screen\n",
      "        turtle = Turtle()   #object of Turtle class\n",
      "        turtle.begin_fill() #start filling color in circle\n",
      "        turtle.color(self._fillcolor)   #color\n",
      "        turtle.up()\n",
      "        turtle.goto(self._xCo,self._yCo)   #set center of circle\n",
      "        turtle.circle(self._radius)   #draw circle of radius self.__radius\n",
      "        turtle.end_fill()   #stop filling\n",
      "        turtle.hideturtle()\n",
      "        done()\n",
      "\n",
      "#creating objects of class circle       \n",
      "c1 = circle()\n",
      "c2 = circle()\n",
      "c3 = circle()\n",
      "\n",
      "#sending the value to set fnction\n",
      "c1.set(15,7,5,\"blue\")\n",
      "c2.set(41,12,7,\"red\")\n",
      "c3.set(65,18,4,\"green\")\n",
      "\n",
      "#draw circle\n",
      "c1.draw()\n",
      "c2.draw()\n",
      "c3.draw()\n",
      "\n",
      "#In the above example the cirlcle's in the book are constructed using 'X' and 'O' but such feature is not available in Python.\n",
      "#So i have created a simple circle filled with color"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.4, Page Number: 226"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Distance:       #Distance class\n",
      "    \n",
      "    def setdist(self,ft,inc):     #set distance to class variables\n",
      "        self.__feet = ft\n",
      "        self.__inches = inc\n",
      "    \n",
      "    def getdist(self):     #get distance from user\n",
      "        self.__feet = input('Enter feet:')\n",
      "        self.__inches = input('Enter inches:')\n",
      "    \n",
      "    def showdist(self):    #display distance\n",
      "        print self.__feet , '\\' -' , self.__inches , '\\\"'\n",
      "\n",
      "#define two distance\n",
      "dist1 = Distance()\n",
      "dist2 = Distance()\n",
      "\n",
      "dist1.setdist(11,6.25)   #set dist1\n",
      "dist2.getdist()     #set dist2 from user\n",
      "\n",
      "#show distances\n",
      "print \"dist1 = \",\n",
      "dist1.showdist()\n",
      "print 'dist2 = ',\n",
      "dist2.showdist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter feet:17\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter inches:5.75\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dist1 =  11 ' - 6.25 \"\n",
        "dist2 =  17 ' - 5.75 \"\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.5, Page Number: 228"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Counter:\n",
      "    \n",
      "    def __init__(self):     #constructor\n",
      "        self.__count = 0\n",
      "    \n",
      "    def inc_count(self):    #increment count\n",
      "        self.__count = self.__count + 1\n",
      "    \n",
      "    def get_count(self):    #return count\n",
      "        return self.__count\n",
      "\n",
      "#define and initialize class objects\n",
      "c1=Counter()\n",
      "c2=Counter()\n",
      "\n",
      "#display count for each object\n",
      "print 'c1 =',c1.get_count()\n",
      "print 'c2 =',c2.get_count()\n",
      "\n",
      "\n",
      "c1.inc_count()    #increment c1\n",
      "c2.inc_count()    #increment c2\n",
      "c2.inc_count()    #increment c2\n",
      "\n",
      "#display count again for each object\n",
      "print 'c1 =',c1.get_count()\n",
      "print 'c2 =',c2.get_count()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "c1 = 0\n",
        "c2 = 0\n",
        "c1 = 1\n",
        "c2 = 2\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.6, Page Number: 231"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from turtle import *       #importing turtles library\n",
      "\n",
      "class circle:          #defining circle class\n",
      "    \n",
      "    def __init__(self,x,y,r,fc):     #constructor for set circle attribute\n",
      "        self._xCo = x\n",
      "        self._yCo = y\n",
      "        self._radius = r\n",
      "        self._fillcolor = fc\n",
      "        \n",
      "    def draw(self):      #draws the circle\n",
      "        setup()\n",
      "        turtle = Turtle()\n",
      "        turtle.begin_fill()\n",
      "        turtle.color(self._fillcolor)\n",
      "        turtle.up()\n",
      "        turtle.goto(self._xCo,self._yCo)\n",
      "        turtle.down()\n",
      "        turtle.circle(self._radius)\n",
      "        turtle.end_fill()\n",
      "        turtle.hideturtle()\n",
      "        done()\n",
      "\n",
      "#creating objects of class circle        \n",
      "c1 = circle(15,7,5,\"blue\")   \n",
      "c2 = circle(41,12,7,\"red\")\n",
      "c3 = circle(65,18,4,\"green\")\n",
      "\n",
      "#draw circle\n",
      "c1.draw()\n",
      "c2.draw()\n",
      "c3.draw()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.7, Page Number: 233"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Distance:                      #Distance class\n",
      "    \n",
      "    def __init__(self,ft=0,inc=0):   #constructor \n",
      "        self.__feet = ft\n",
      "        self.__inches = inc\n",
      "    \n",
      "    def getdist(self):               #get length from user\n",
      "        self.__feet = input('Enter feet:')\n",
      "        self.__inches = input('Enter inches:')\n",
      "    \n",
      "    def showdist(self):              #display distance\n",
      "        print self.__feet , '\\' -' , self.__inches , '\\\"'\n",
      "    \n",
      "    def add_dist(self,d2,d3):        #add length d2 and d3\n",
      "        self.__inches = d2.__inches + d3.__inches      #add inches\n",
      "        self.__feet = 0\n",
      "        if self.__inches >= 12.0:    #if total exceeds 12.0\n",
      "            self.__inches = self.__inches - 12.0       #then decrease inches by 12.0\n",
      "            self.__feet = self.__feet + 1              #and increase feet by 1\n",
      "        self.__feet = self.__feet + d2.__feet + d3.__feet       #add the feet\n",
      "\n",
      "#define two length\n",
      "dist1 = Distance()\n",
      "dist3 = Distance()\n",
      "\n",
      "#define and initialize dist2\n",
      "dist2 = Distance(11,6.25)\n",
      "\n",
      "#get dist1 from user\n",
      "dist1.getdist()\n",
      "\n",
      "#dist3 = dist1 + dist2\n",
      "dist3.add_dist(dist1,dist2)\n",
      "\n",
      "#display all lengths\n",
      "print 'dist1 = ',\n",
      "dist1.showdist()\n",
      "print 'dist2 = ',\n",
      "dist2.showdist()\n",
      "print 'dist3 = ',\n",
      "dist3.showdist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter feet:17\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter inches:5.75\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dist1 =  17 ' - 5.75 \"\n",
        "dist2 =  11 ' - 6.25 \"\n",
        "dist3 =  29 ' - 0.0 \"\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.8, Page Number: 238"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Distance:        #Distance  class\n",
      "    \n",
      "    def __init__(self,ft=0,inc=0):  #overloaded constructor that takes no arguments or two args or one object(copy constructor)\n",
      "        if isinstance(ft,int):\n",
      "            self.__feet = ft\n",
      "            self.__inches = inc\n",
      "        else:\n",
      "            self.__feet = ft.__feet\n",
      "            self.__inches = ft.__inches\n",
      "            \n",
      "    def getdist(self):      #get length from user\n",
      "        self.__feet = input('Enter feet:')\n",
      "        self.__inches = input('Enter inches:')\n",
      "    \n",
      "    def showdist(self):     #display distance\n",
      "        print self.__feet , '\\' -' , self.__inches , '\\\"'\n",
      "\n",
      "#two argument constructor\n",
      "dist1 = Distance(11,6.25)\n",
      "\n",
      "#one argument(object) constructor explicitly pass\n",
      "dist2 = Distance(dist1)\n",
      "\n",
      "#also one argument(object) constructor implicitly pass\n",
      "dist3 = dist1\n",
      "\n",
      "#display all lengths\n",
      "print 'dist1 = ',\n",
      "dist1.showdist()\n",
      "print 'dist2 = ',\n",
      "dist2.showdist()\n",
      "print 'dist3 = ',\n",
      "dist3.showdist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dist1 =  11 ' - 6.25 \"\n",
        "dist2 =  11 ' - 6.25 \"\n",
        "dist3 =  11 ' - 6.25 \"\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.9, Page Number: 240"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Distance:        #Distance class\n",
      "    \n",
      "    def __init__(self,ft=0,inc=0):     #constructor \n",
      "        self.__feet = ft\n",
      "        self.__inches = inc\n",
      "    \n",
      "    def getdist(self):         #get length from user\n",
      "        self.__feet = input('Enter feet:')\n",
      "        self.__inches = input('Enter inches:')\n",
      "    \n",
      "    def showdist(self):        #display distance\n",
      "        print self.__feet , '\\' -' , self.__inches , '\\\"'\n",
      "    \n",
      "    def add_dist(self,d2):     #add this length to d2 and return object\n",
      "        temp = Distance()      #temporary object\n",
      "        temp.__inches = self.__inches + d2.__inches\n",
      "        if temp.__inches >= 12.0:\n",
      "            temp.__inches = temp.__inches - 12.0\n",
      "            temp.__feet = 1\n",
      "        temp.__feet = temp.__feet + self.__feet + d2.__feet\n",
      "        return temp            #return sum as object\n",
      "\n",
      "#define two length\n",
      "dist1 = Distance()\n",
      "dist3 = Distance()\n",
      "\n",
      "#define and initialize dist2\n",
      "dist2 = Distance(11,6.25)\n",
      "\n",
      "#get dist1 from user\n",
      "dist1.getdist()\n",
      "\n",
      "#dist3 = dist1 + dist2\n",
      "dist3 = dist1.add_dist(dist2)\n",
      "\n",
      "#display all lengths\n",
      "print 'dist1 = ',\n",
      "dist1.showdist()\n",
      "print 'dist2 = ',\n",
      "dist2.showdist()\n",
      "print 'dist3 = ',\n",
      "dist3.showdist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter feet:17\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter inches:5.75\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dist1 =  17 ' - 5.75 \"\n",
        "dist2 =  11 ' - 6.25 \"\n",
        "dist3 =  29 ' - 0.0 \"\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.10, Page Number: 243"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Suit = [\"clubs\",\"diamonds\",\"hearts\",\"spades\"]  \n",
      "\n",
      "(clubs,diamonds,hearts,spades) = (0,1,2,3)    #Atteching the names with number  \n",
      "\n",
      "\n",
      "#from 2 to 10 are integers without names\n",
      "jack = 11      \n",
      "queen = 12 \n",
      "king = 13\n",
      "ace = 14\n",
      "\n",
      "\n",
      "class card:       \n",
      "    \n",
      "    def __init__(self,n=None,s=None):       #constructor\n",
      "        self.__number = n                   #2 to 10, jack, queen, king, ace\n",
      "        self.__suit = s                     #clubs, diamonds, hearts, spades\n",
      "    \n",
      "    def display(self):                      #display the cards\n",
      "        \n",
      "        if self.__number >= 2 and self.__number<=10:\n",
      "            print self.__number , 'of',\n",
      "            \n",
      "        else:\n",
      "            if self.__number == jack:\n",
      "                print 'jack of',\n",
      "            elif self.__number == queen:\n",
      "                print 'queen of',\n",
      "            elif self.__number == king:\n",
      "                print 'king of',\n",
      "            else:\n",
      "                print 'ace of',\n",
      "                \n",
      "        if self.__suit == clubs:\n",
      "            print 'clubs'\n",
      "        elif self.__suit == diamonds:\n",
      "            print 'diamonds'\n",
      "        elif self.__suit == hearts:\n",
      "            print 'hearts'\n",
      "        else:\n",
      "            print 'spades'\n",
      "    \n",
      "    def isEqual(self,c2):           #return 1 if cards equal\n",
      "        \n",
      "        if self.__number == c2.__number and self.__suit == c2.__suit:\n",
      "            return 1\n",
      "        else:\n",
      "            return 0\n",
      "\n",
      "\n",
      "#define various cards\n",
      "temp = card()\n",
      "chosen = card()\n",
      "prize = card()\n",
      "\n",
      "\n",
      "#define and initialize card1\n",
      "card1 = card(7,clubs)\n",
      "print 'card 1 is the',\n",
      "card1.display()    #display card1\n",
      "\n",
      "#define and initialize card2\n",
      "card2 = card(jack,hearts)\n",
      "print 'card 2 is the',\n",
      "card2.display()    #display card2\n",
      "\n",
      "#define and initialize card3\n",
      "card3 = card(ace,spades)\n",
      "print 'card 3 is the',\n",
      "card3.display()    #display card3\n",
      "\n",
      "\n",
      "#prize is the card to guess\n",
      "prize = card3\n",
      "\n",
      "\n",
      "#swapping cards\n",
      "print 'I\\'m swapping card 1 and card 3'\n",
      "temp = card3\n",
      "card3 = card1\n",
      "card1 = temp\n",
      "\n",
      "print 'I\\'m swapping card 2 and card 3'\n",
      "temp = card2\n",
      "card3 = card2\n",
      "card2 = temp\n",
      "\n",
      "print 'I\\'m swapping card 1 and card 2'\n",
      "temp = card2\n",
      "card2 = card1\n",
      "card1 = temp\n",
      "\n",
      "print 'Now, where (1,2, or 3) is the',\n",
      "prize.display()          #display prize\n",
      "print '?'\n",
      "\n",
      "position = input()       #get user's guess of position\n",
      "\n",
      "\n",
      "#set chosen to user's choice \n",
      "if position == 1:\n",
      "    chosen = card1\n",
      "elif position == 2:\n",
      "    chosen = card2\n",
      "else:\n",
      "    chosen = card3\n",
      "\n",
      "#is chosen card the prize?\n",
      "\n",
      "x=chosen.isEqual(prize)\n",
      "\n",
      "if x==1:\n",
      "    print 'That\\'s right! You win!'\n",
      "else:\n",
      "    print 'Sorry. You lose.'\n",
      "\n",
      "print 'You choose the',\n",
      "\n",
      "#display chosen card\n",
      "chosen.display()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "card 1 is the 7 of clubs\n",
        "card 2 is the jack of hearts\n",
        "card 3 is the ace of spades\n",
        "I'm swapping card 1 and card 3\n",
        "I'm swapping card 2 and card 3\n",
        "I'm swapping card 1 and card 2\n",
        "Now, where (1,2, or 3) is the ace of spades\n",
        "?\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "That's right! You win!\n",
        "You choose the ace of spades\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.11, Page Number: 249"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class foo: \n",
      "    \n",
      "    __count = 0     #only one data item for all objects\n",
      "    \n",
      "    def __init__(self):\n",
      "        foo.__count = foo.__count + 1     #increment count when object created\n",
      "    \n",
      "    def getcount(self):        #returns count\n",
      "        return foo.__count\n",
      "\n",
      "#create three objecs\n",
      "f1 = foo()\n",
      "f2 = foo()\n",
      "f3 = foo()\n",
      "\n",
      "#Each object displays the same count value\n",
      "print 'count is', f1.getcount()\n",
      "print 'count is', f2.getcount()\n",
      "print 'count is', f3.getcount()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "count is 3\n",
        "count is 3\n",
        "count is 3\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.12, Page Number: 253"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Distance:         #Distance class\n",
      "    \n",
      "    def __init__(self,ft=0,inc=0.0):        #constructor \n",
      "        self.__feet = ft\n",
      "        self.__inches = inc\n",
      "    \n",
      "    def getdist(self):              #get length from user\n",
      "        self.__feet = input('Enter feet:')\n",
      "        self.__inches = input('Enter inches:')\n",
      "    \n",
      "    def showdist(self):             #display distance\n",
      "        print self.__feet , '\\' -' , self.__inches , '\\\"'\n",
      "\n",
      "#There's no const keyword \n",
      "    \n",
      "    def add_dist(self,d2):          #add this length to d2 and return object\n",
      "        \n",
      "        temp = Distance()\n",
      "        temp.__inches = self.__inches + d2.__inches\n",
      "        \n",
      "        if temp.__inches >= 12.0:\n",
      "            temp.__inches = temp.__inches - 12.0\n",
      "            temp.__feet = 1\n",
      "            \n",
      "        temp.__feet = temp.__feet + self.__feet + d2.__feet\n",
      "        \n",
      "        return temp                  #return sum as object\n",
      "    \n",
      "dist1 = Distance()\n",
      "dist3 = Distance()\n",
      "dist2 = Distance(11,6.25)\n",
      "\n",
      "dist1.getdist()\n",
      "\n",
      "dist3 = dist1.add_dist(dist2)\n",
      "\n",
      "print 'dist1 = ',\n",
      "dist1.showdist()\n",
      "print 'dist2 = ',\n",
      "dist2.showdist()\n",
      "print 'dist3 = ',\n",
      "dist3.showdist()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter feet:17\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter inches:5.75\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dist1 =  17 ' - 5.75 \"\n",
        "dist2 =  11 ' - 6.25 \"\n",
        "dist3 =  29 ' - 0.0 \"\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 6.13, Page Number: 255"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Distance:\n",
      "    \n",
      "    def __init__(self,ft,inc):\n",
      "        self.__feet = ft\n",
      "        self.__inches = inc\n",
      "    \n",
      "    def getdist(self):\n",
      "        self.__feet = input('Enter feet:')\n",
      "        self.__inches = input('Enter inches:')\n",
      "    \n",
      "    def showdist(self):\n",
      "        print self.__feet , '\\' -' , self.__inches , '\\\"'\n",
      "\n",
      "football = Distance(300,0)\n",
      "\n",
      "print 'football = ',\n",
      "football.showdist()\n",
      "\n",
      "#There's no const keyword in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "football =  300 ' - 0 \"\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}