Chapter 5 : Structured Programming

Example 5.1, Page No 116

In [1]:
def addTwoNumbers(operand1,operand2):
    return operand1+operand2

print "Nothing happenning here"
Nothing happenning here

Example 5.2, Page No 118

In [3]:
def addTwoNumbers(num1,num2):
    return num1+num2

def subtractTwoNumber(num1,num2):
    return num1-num2

print "Nothing happenning here"
Nothing happenning here

Example 5.3, Page No 119

In [1]:
def addTwoNumbers(operand1,operand2):
    return operand1+operand2


iResult= addTwoNumbers(5,5)

Example 5.4, Page No 120

In [7]:
def addTwoNumbers(operand1,operand2):
    return operand1+operand2

print "The result is : ",addTwoNumbers(5,5)
The result is :  10

Example 5.5, Page No 120

In [9]:
def addTwoNumbers(operand1,operand2):
    return operand1+operand2

num1=int(raw_input("Enter the first number: "))
num2=int(raw_input("Enter the second number: "))
print "The result is: ",addTwoNumbers(num1,num2)
Enter the first number: 57
Enter the second number: 43
The result is:  100

Example 5.6, Page No 121

In [11]:
def printReportHeader():
    print "\n Column1 \t Column2 \t Column3 \t Column4 \n"
    
printReportHeader()
 Column1 	 Column2 	 Column3 	 Column4 

Example 5.7, Page No 122

In [12]:
num1=int(raw_input("Enter a number: "))
print "You entered ",num1
Enter a number: 10
You entered  10

Example 5.8, Page No 123

In [17]:
def getSecondNumber():
    num1=int(raw_input("Enter Second number: "))
    return num1

num1=int(raw_input("Enter a number: "))
num2=getSecondNumber()
print "you entered "+str(num1)+" and ",str(num2)
Enter a number: 28
Enter Second number: 35
you entered 28 and  35

Example 5.9, Page No 124

In [19]:
iLuckyNumber=int()
def printLuckyNumber():
    print "Your lucky number is: ",iLuckyNumber


iLuckyNumber=int(raw_input("Enter your lucky number: "))
printLuckyNumber()
Enter your lucky number: 10
Your lucky number is:  10

Example 5.10, Page No 126

In [3]:
import os
import time
def sportsQuestion():
    print "Sports Question"
    print "What University did NFL star Deon Sanders attend?"
    print "1 \t University of Miami"
    print "2 \t California State University"
    print "3 \t Indiana University"
    print "4 \t Florida State University"
    iAnswer=int(raw_input("Enter your selection :"))
    return iAnswer

def geographyQuestion():
    print "Geography Question"
    print "What is the state capitol of Florida?"
    print "1 \t Pensecola"
    print "2 \t Tallahassee"
    print "3 \t Jacksonville"
    print "4 \t Miami"
    iAnswer=int(raw_input("Enter your selection :"))
    return iAnswer
def pause(inNum):
    iCurrentTime=0
    iElapsedTime=0
    
    iCurrentTime=int(round(time.time()*1000))
    
    while((iElapsedTime-iCurrentTime)<inNum):
        iElapsedTime=int(round(time.time()*1000))

giResponse=0
while(giResponse !=3):
    os.system('cls')
    print "\n\tTHE TRIVIA GAME\n\n"
    print "1\tSports\n"
    print "2\tGeography\n"
    print "3\tQuit\n"
    giResponse=int(raw_input("Enter your Selection: "))
    
    if(giResponse==1):
        if(sportsQuestion()==4):
            print "\nCorrect\n"
        else:
            print "\nIncorrect\n"
            
        pause(2)
        break
    elif(giResponse==2):
        if(geographyQuestion()==2):
            print "\nCorrect\n"
        else:
            print "\nIncorrect\n"
        
        pause(2)
        break
            
            
    
	THE TRIVIA GAME


1	Sports

2	Geography

3	Quit

Enter your Selection: 1
Sports Question
What University did NFL star Deon Sanders attend?
1 	 University of Miami
2 	 California State University
3 	 Indiana University
4 	 Florida State University
Enter your selection :4

Correct