Chapter 4: Structures

Example 4.1, Page Number: 132

In [1]:
class part:                 #declare a structure
    modelnumber = None      #ID number of widget
    partnumber = None       #ID number of widget part
    cost = None             #cost of part

part1 = part()              #define a strcture variable

part1.modelnumber = 6244    #give values to structue member
part1.partnumber = 373
part1.cost = 217.55

                            #display structure member
print 'Model %d,'    %part1.modelnumber,
print 'part %d,'     %part1.partnumber,
print 'costs $%.2f'  %part1.cost,'\n'
Model 6244, part 373, costs $217.55 

Example 4.2, Page Number: 138

In [2]:
class part:                 #declare a structure
    modelnumber = None      #ID number of widget
    partnumber = None       #ID number of widget part
    cost = None             #cost of part

part1 = part()
                           #initialize structure variable
part1.modelnumber = 6244
part1.partnumber = 373
part1.cost = 217.55

                        #display first variable
print 'Model %d,'    %part1.modelnumber,
print 'part %d,'     %part1.partnumber,
print 'costs $%.2f'  %part1.cost,'\n',

part2 = part1           #assign first variable to second

                        #display second variable
print 'Model %d,'    %part2.modelnumber,
print 'part %d,'     %part2.partnumber,
print 'costs $%.2f'  %part2.cost,'\n'
Model 6244, part 373, costs $217.55 
Model 6244, part 373, costs $217.55 

Example 4.3, Page Number: 139

In [3]:
class Distance:         #Distance structure
    feet = None
    inches = None

d1 = Distance()         #define three length
d3 = Distance()
d2 = Distance()

d2.feet = 11            #initialize second length
d2.inches = 6.25

                        #get lenght d1 from user
d1.feet = input("Enter feet: ")
d1.inches = input("Enter inches: ")

                        #add length d1 and d2 to get d3
d3.inches = d1.inches + d2.inches         #add the inches


d3.feet = 0                     #initially feet is zero

if d3.inches>=12.0:             #if total exceeds 12.0
    d3.inches -= 12.0           #then decrease inches by 12.0
    d3.feet += 1                #and increase feet by 1

d3.feet += d1.feet + d2.feet    #add the feet

                                #display all length
print d1.feet,'\' -',d1.inches,'\" +',
print d2.feet,'\' -',d2.inches,'\" =',
print d3.feet,'\' -',d3.inches,'\" \n',
Enter feet: 10
Enter inches: 6.75
10 ' - 6.75 " + 11 ' - 6.25 " = 22 ' - 1.0 " 

Example 4.4, Page Number: 141

In [4]:
class Distance:                #distance structure
    feet = None
    inches = None

class Room:                    #rectangular area
    length = Distance()        #length of rectangle
    width = Distance()         #width of rectangle
        
dining = Room()                #define a room

dining.length.feet = 13        #assign values 
dining.length.inches = 6.5
dining.width.feet = 10
dining.width.inches = 0.0

                               #convet length and width
l = dining.length.feet + dining.length.inches/12
w = dining.width.feet + dining.width.inches/12

                               #find area and display it
print 'Dining room area is',l*w,'square feet\n'
Dining room area is 135.416666667 square feet

Example 4.5, Page Number: 145

In [5]:
clubs = 0                   #suits
diamonds = 1
hearts = 2
spades = 3

#from 2 to 10 are integers without names
jack = 11                  #face cards
queen = 12 
king = 13
ace = 14


class card:
        number = None                   #2 to 10, jack, queen, king, ace
        suit = None                     #clubs, diamonds, hearts, spades
    

#define various cards
temp = card()
chosen = card()
prize = card()


#define and initialize card1
card1 = card()
card1.number = 7
card1.suit = clubs
print 'card 1 is the 7 of clubs'


#define and initialize card2
card2 = card()
card2.number = jack
card2.suit = hearts
print 'card 2 is the jack of hearts'

#define and initialize card3
card3 = card()
card3.number = ace
card3.suit = spades
print 'card 3 is the ace of spades'


#prize is the card to guess
prize = card3        #copy this card, to remember it


#swapping cards
print 'I\'m swapping card 1 and card 3'
temp = card3
card3 = card1
card1 = temp

print 'I\'m swapping card 2 and card 3'
temp = card2
card3 = card2
card2 = temp

print 'I\'m swapping card 1 and card 2'
temp = card2
card2 = card1
card1 = temp


print 'Now, where (1,2, or 3) is the ace of spades? ',

position = input()       #get user's guess of position


#set chosen to user's choice 
if position == 1:
    chosen = card1
elif position == 2:
    chosen = card2
else:
    chosen = card3
    
    
if chosen.number == prize.number and chosen.suit == prize.suit:       #compare cards
    print 'That\'s right! You win!'
else:
    print 'Sorry. You lose.'
card 1 is the 7 of clubs
card 2 is the jack of hearts
card 3 is the ace of spades
I'm swapping card 1 and card 3
I'm swapping card 2 and card 3
I'm swapping card 1 and card 2
Now, where (1,2, or 3) is the ace of spades? 3
 Sorry. You lose.

Example 4.6, Page Number: 148

In [6]:
(Sun,Mon,Tue,Wed,Thu,Fri,Sat) = (0,1,2,3,4,5,6)                   #specify enum type

day1 = Mon               #define variables and assign values
day2 = Thu

diff = day2 - day1        #can do integer arithmetic
print 'Days between =',diff

if day1<day2:             #can do comprisons
    print 'day1 comes before day2\n'
Days between = 3
day1 comes before day2

Example 4.7, Page Number: 152

In [7]:
(NO , YES) = (0,1)         #N0 = 0, YES = 1

isWord = NO               #YES when in a word, NO when in whitespace
ch = 'a'                  #character read from keyboard
wordcount = 1             #counts spaces between words

print 'Enter a phrase'
ch = raw_input()          #geting the line

for j in range(len(ch)):           #for loop till line ends
    
    if ch[j] == ' ':               #if it's a space,
        if isWord == YES:          #and going  word, then it's end of word
            wordcount += 1         #count a word
            isWord == NO           #reset flag
            
    else:                          #otherwise
        if isWord == NO:           
            isWord = YES           #set flag

print '--- Word count is',wordcount,'---'        #display result
Enter a phrase
For While and do
--- Word count is 4 ---

Example 4.8, Page Number: 153

In [8]:
#from 2 to 10 are integers without names
jack = 11      
queen = 12 
king = 13
ace = 14

(clubs,diamonds,hearts,spades) = (0,1,2,3)

class card:
        number = None                   #2 to 10, jack, queen, king, ace
        suit = None                   #clubs, diamonds, hearts, spades
    

#define various cards
temp = card()
chosen = card()
prize = card()


#define and initialize card1
card1 = card()
card1.number = 7
card1.suit = clubs
print 'card 1 is the 7 of clubs'


#define and initialize card2
card2 = card()
card2.number = jack
card2.suit = hearts
print 'card 2 is the jack of hearts'

#define and initialize card3
card3 = card()
card3.number = ace
card3.suit = spades
print 'card 3 is the ace of spades'


#prize is the card to guess
prize = card3


#swapping cards
print 'I\'m swapping card 1 and card 3'
temp = card3
card3 = card1
card1 = temp

print 'I\'m swapping card 2 and card 3'
temp = card2
card3 = card2
card2 = temp

print 'I\'m swapping card 1 and card 2'
temp = card2
card2 = card1
card1 = temp


print 'Now, where (1,2, or 3) is the ace of spades? ',

position = input()       #get user's guess of position


#set chosen to user's choice 
if position == 1:
    chosen = card1
elif position == 2:
    chosen = card2
else:
    chosen = card3
    
                                                        #compare cards
if chosen.number == prize.number and chosen.suit == prize.suit:
    print 'That\'s right! You win!'
else:
    print 'Sorry. You lose.'
card 1 is the 7 of clubs
card 2 is the jack of hearts
card 3 is the ace of spades
I'm swapping card 1 and card 3
I'm swapping card 2 and card 3
I'm swapping card 1 and card 2
Now, where (1,2, or 3) is the ace of spades? 2
 That's right! You win!
In [ ]: