Chapter 14: The Road Ahead: Structures and Classes

example 14.1, page no. 317

In [1]:
class Person:
    name = ""
    height = 0

example 14.2, page no. 318

In [2]:
class Person:
    name = ""
    height = 0

MAX = 3
p = []

for x in range(MAX):
    per = Person()
    print "Enter person's name: ",
    per.name = raw_input()
    print "Enter height in inches: ",
    per.height = int(raw_input())
    p.append(per)

print "Outputting person data\n";
print "======================\n";
for x in range(MAX):
    print "Person ", x + 1, "'s name is ", p[x].name, " and height is ", p[x].height
Enter person's name: Jeff
 Enter height in inches: 50
 Enter person's name: Kent
 Enter height in inches: 50
 Enter person's name: Jefff
 Enter height in inches: 40
 Outputting person data

======================

Person  1 's name is  Jeff  and height is  50
Person  2 's name is  Kent  and height is  50
Person  3 's name is  Jefff  and height is  40

example 14.3, page no. 320

In [3]:
class Person:
    name = ""
    height = 0

MAX = 3
p1 = Person()
print "The person's name is ", p1.name, " and height is ", p1.height
The person's name is    and height is  0

example 14.4, page no. 322

In [4]:
class Person:
    def __init__(self):
        self.name = "no name assigned"
        self.height = -1

MAX = 3
p1 = Person()
print "The person's name is ", p1.name, " and height is ", p1.height
The person's name is  no name assigned  and height is  -1

example 14.5, page no. 323

In [5]:
class Person:
    def __init__(self, s="no name assigned", h=-1):
        self.name = s
        self.height = h

MAX = 3
print "Enter a person's name: ",
s = raw_input()
print "Enter height in inches: ",
h = int(raw_input())
p1 = Person(s, h)
print "The person's name is ", p1.name, " and height is ", p1.height
Enter a person's name: Jeff
 Enter height in inches: 39
 The person's name is  Jeff  and height is  39

example 14.6, page no. 324

In [6]:
class Person:
    def __init__(self, s="no name assigned", h=-1):
        self.name = s
        self.height = h

MAX = 3
print "Enter a person's name: ",
s = raw_input()
print "Enter height in inches: ",
h = int(raw_input())
p1 = Person(s, h)
print "The person's name is ", p1.name, " and height is ", p1.height
Enter a person's name: Jeff
 Enter height in inches: 50
 The person's name is  Jeff  and height is  50

example 14.7, page no. 325

In [7]:
class Person:
    name = ""
    height = ""
    def setValues(self, pers):
        print "Enter person's name: ",
        pers.name = raw_input()
        print "Enter height in inches: ",
        pers.height = int(raw_input())
    def getValues(self, pers):
        print "Person's name is ", pers.name, " height is ", pers.height

p1 = Person()
p1.setValues(p1)
print "Outputting person data"
print "======================"
p1.getValues(p1)
Enter person's name: Jeff
 Enter height in inches: 60
 Outputting person data
======================
Person's name is  Jeff  height is  60

example 14.8, page no. 326

In [8]:
class Date:
    month = 0
    day = 0
    year = 0

class Person:
    name = ""
    height = 0
    bDay = Date()
    def setValues(self, pers):
        print "Enter person's name: ",
        pers.name = raw_input()
        print "Enter height in inches: ",
        pers.height = int(raw_input())
        print "Enter birthdate"
        pers.bDay.month = int(raw_input("Month: "))
        pers.bDay.day  = int(raw_input("Day: "))
        pers.bDay.year = int(raw_input("Year: "))
    def getValues(self, pers):
        print "Person's name: ", pers.name
        print "Person's height", pers.height
        print "Person's birthday in mm/dd/yyyy format is: ", pers.bDay.month, pers.bDay.day, pers.bDay.year

p1 = Person()
p1.setValues(p1)
print "Outputting person data"
print "======================"
p1.getValues(p1)
Enter person's name: Jeff
 Enter height in inches: 59
 Enter birthdate
Month: 20
Day: 05
Year: 1999
Outputting person data
======================
Person's name:  Jeff
Person's height 59
Person's birthday in mm/dd/yyyy format is:  20 5 1999

example 14.9, page no. 330

In [9]:
class Person:
    name = ""
    height = ""
    def setValues(self, pers):
        print "Enter person's name: ",
        pers.name = raw_input()
        print "Enter height in inches: ",
        pers.height = int(raw_input())
    def getValues(self, pers):
        print "Person's name is ", pers.name, " height is ", pers.height

p1 = Person()
p1.setValues(p1)
print "Outputting person data"
print "======================"
p1.getValues(p1)
Enter person's name: JEff
 Enter height in inches: 50
 Outputting person data
======================
Person's name is  JEff  height is  50

example 14.10, page no. 331

In [10]:
class Person:
    name = ""
    height = ""
    def setValues(self, pers):
        print "Enter person's name: ",
        pers.name = raw_input()
        print "Enter height in inches: ",
        pers.height = int(raw_input())
    def getValues(self):
        print "Person's name is ", self.getName(), " height is ", self.getHeight()
    def getName(self):
        return self.name
    def getHeight(self):
        return self.height

p1 = Person()
p1.setValues(p1)
print "Outputting person data"
print "======================"
p1.getValues()
Enter person's name: Jeff
 Enter height in inches: 60
 Outputting person data
======================
Person's name is  Jeff  height is  60