Chapter 3 CONDITIONS

Example 3.1, Page No. 58

In [1]:
print "\tAC Control Unit"
print "1\tTurn the AC on"
print "2\tTurn the AC off"
iResponse=int(raw_input("Enter your selection::"))
if iResponse==1:
    print "AC is now on"
if iResponse==2:
    print "AC is now off"
	AC Control Unit
1	Turn the AC on
2	Turn the AC off
Enter your selection::1
AC is now on

Example 3.2, Page No. 59

In [3]:
print "\tAC Control Unit"
print "a\tTurn the AC on"
print "b\tTurn the AC off"
cResponse=raw_input("Enter your selection::")
if cResponse=='a':
    print "AC is now on"
if cResponse=='b':
    print "AC is now off"
	AC Control Unit
a	Turn the AC on
b	Turn the AC off
Enter your selection::b
AC is now off

Example 3.3, Page No. 61

In [4]:
fBalance = 100.25
print "\tATM"
print "1\tDeposit Funds"
print "2\tWithdraw Funds"
iSelection=int(raw_input("Enter Your Selection: "))
if iSelection==1:
    fTransAmount=int(raw_input("Enter fund amount to deposit: "))
    print "Your new Balance is: ", fBalance+fTransAmount
if iSelection==2:
    fTransAmount=int(raw_input("Enter fund amount to withdraw: "))
    if fTransAmout>fBalance:
        print "Insufficient Funds"
    else:
        print "Your new balance is ", fBalance-fTransAmount
	ATM
1	Deposit Funds
2	Withdraw Funds
Enter Your Selection: 1
Enter fund amount to deposit: 200
Your new Balance is:  300.25

Example 3.4, Page No. 67

In [5]:
cResponse=raw_input("Enter a letter A: ")
if cResponse=='A':
    print "Correct response"
else:
    print "Incorrect response"
Enter a letter A: a
Incorrect response

Example 3.5, Page No.69

In [3]:
iResponse=int(raw_input("Enter a numeer from 1 to 10: "))
if iResponse<1 or iResponse>10:
    print "Number not in Range"
else:
    print "Thank You"
Enter a numeer from 1 to 10: 5
Thank You

Example 3.6, Page No. 70

In [8]:
cResponse=raw_input("Please Enter a letter: ")
if not cResponse.isdigit():
    print "Thank You"
else:
    print "You did not enter a letter"
Please Enter a letter: 5
You did not enter a letter

Example 3.7, Page No. 70

In [9]:
cResponse=raw_input("Please Enter a letter: ")
if cResponse.isdigit():
    print "Thank You"
else:
    print "You did not enter a digit"
Please Enter a letter: a
You did not enter a digit

Example 3.8, Page No.72

In [1]:
print "1\tSports"
print "2\tGeography"
print "3\tMusic"
print "4\tWorld Events"
iResponse=int(raw_input("Please select a category (1-4): "))
if iResponse==1:
    print "You selected sports questions"
elif iResponse==2:
    print "You selected geography questions"
elif iResponse==3:
    print "You selected Music questions"
elif iResponse==4:
    print "You selected World event questions"
1	Sports
2	Geography
3	Music
4	World Events
Please select a category (1-4): 4
You selected World event questions

Example 3.9, Page No. 75

In [6]:
import random
iRandomNum=(random.randint(0,100)%10)+1
iResponse=int(raw_input("Guess a number between 1 and 10: "))
if iResponse==iRandomNum:
    print "You guessed right"
else:
    print "Sorry, you guess wrong"
    print "The correct guess was ",iRandomNum
Guess a number between 1 and 10: 2
Sorry, you guess wrong
The correct guess was  3

Example 3.10, Page No. 77

In [13]:
import random
iRandomNum=(random.randint(0,100)%4)+1
print "Fortune Cookie - Chapter 3"
if iRandomNum==1:
    print "You will meet a new friend"
elif iRandomNum==2:
    print "You will enjoy a long and happy life"
elif  iRandomNum==3:
    print "Opportunity knocks softly. Can you hear it?"
elif iRandomNum==4:
    print "You'll be financially rewarded for your good deeds"

print "Lucky lotto numbers"
print (random.randint(0,100)%49+1)
print (random.randint(0,100)%49+1)
print (random.randint(0,100)%49+1)
print (random.randint(0,100)%49+1)
print (random.randint(0,100)%49+1)
print (random.randint(0,100)%49+1)
Fortune Cookie - Chapter 3
You will enjoy a long and happy life
Lucky lotto numbers
45
17
9
23
2
15