Chapter 3: Making Decisions¶

Program 3.1, page no. 88¶

In :
number = 0;
print "\nEnter an integer between 1 and 10: ",
number = int(raw_input())

if(number > 5):
print "You entered %d which is greater than 5\n" % number
if(number < 6):
print "You entered %d which is less than 6\n" % number
Enter an integer between 1 and 10: 6
You entered 6 which is greater than 5

Program 3.2, page no. 91¶

In :
unit_price = 3.50
print "Enter the number that you want to buy: ",
quantity = int(raw_input())
if(quantity > 10):
total = quantity*unit_price*0.95 # 5% discount
print "The price for %d is $%.2f\n" % (quantity, total) else: total = quantity*unit_price #no discount print "The price for %d is$%.2f\n" % (quantity, total)
Enter the number that you want to buy: 20
The price for 20 is $66.50 Program 3.3, page no. 95¶ In : test = 0.0 print "Enter an integer: ", test = int(raw_input()) if(test % 2 == 0): print "The number %ld is even" %test if((test/2) % 2 == 0): print "Half of %d is also even" %test print "That's interesting isn't it?" else: print "The number %d is odd\n" %test Enter an integer: 20 The number 20 is even Half of 20 is also even That's interesting isn't it? Program 3.4, page no. 98¶ In : print "Enter an uppercase letter:", letter = raw_input() if(ord(letter) >= ord('A')): if(ord(letter) <= ord('Z')): letter = chr( ord(letter) - ord('A') + ord('a')) print "You entered an uppercase %c" %letter else: print "Try using the shift key! I want a capital letter.\n" Enter an uppercase letter:G You entered an uppercase g Program 3.5, page no. 103¶ In : print "Enter an uppercase letter:", letter = raw_input() if( (ord(letter) >= ord('A')) and (ord(letter) <= ord('Z')) ): letter = chr( ord(letter) - ord('A') + ord('a')) print "You entered an uppercase %c" %letter else: print "You did not enter an uppercase letter." Enter an uppercase letter:j You did not enter an uppercase letter. Program 3.6, page no. 105¶ In : unit_price = 3.50 discount1 = 0.05 discount2 = 0.1 discount3 = 0.15 total_price = 0.0 print "Enter the number that you want to buy:", quantity = int(raw_input()) if(quantity >50): total_price = quantity*unit_price*(1.0-discount3) elif(quantity > 20): total_price = quantity*unit_price*(1.0-discount2) elif(quantity > 10): total_price = quantity*unit_price*(1.0-discount1) else: total_price = quantity*unit_price*(1.0-0.0) print "The price for %d is$%.2f\n" %(quantity, total_price)
Enter the number that you want to buy:60
The price for 60 is \$178.50

Program 3.7, page no. 109¶

In :
age = 0
college = 0
subject = 0
interview = False # true for accept, false for reject

print "What college? 1 for Harvard, 2 for Yale, 3 for other: ",
college = int(raw_input())
print "What subject? 1 for Chemistry, 2 for economics, 3 for other: ",
subject = int(raw_input())
print "How old is the applicant? ",
age = int(raw_input())

if( (age>25 and subject==1) and (college==3 or college == 1) ):
interview = True
if(college == 2 and subject == 1):
interview = True
if(college == 1 and subject == 2 and not(age > 28)):
interview = True
if(college == 2 and (subject == 2 or subject == 3) and age > 25):
interview = True

if(interview):
print "Give 'em an interview"
else:
print "Reject 'em"
What college? 1 for Harvard, 2 for Yale, 3 for other: 2
What subject? 1 for Chemistry, 2 for economics, 3 for other: 1
How old is the applicant? 24
Give 'em an interview

Program 3.8, page no. 117¶

In :
print "Pick a number between 1 and 10 and you may win a prize! ",
choice = int(raw_input())
if((choice > 10) or (choice < 1)):
choice = 11
if choice == 7:
print "Congratulations!"
print "You win the collected works of Amos Gruntfuttock."
elif choice == 2:
print "You win the folding thermometer-pen-watch-umbrella."
elif choice == 8:
print "You win the lifetime supply of aspirin tablets."
elif choice == 11:
print "Try between 1 and 10. You wasted your guess.",
else:
print "Sorry, you lose.\n"
Pick a number between 1 and 10 and you may win a prize! 7
Congratulations!
You win the collected works of Amos Gruntfuttock.

Program 3.9, page no. 119¶

In :
print "Enter Y or N: ",

print "You responded in the affirmative."
print "You responded in the negative."
else:
print "You did not respond correctly. . ."
Enter Y or N: y
You responded in the affirmative.

Program 3.10, page no. 128¶

In :
original = 0xABC
result = 0

print "original = %X" % original

original = original >> 4
result = result << 4

original = original >> 4
result = result << 4
print "result = %X" % result
original = ABC
result = CBA

Program 3.11, page no. 132¶

In :
print "Enter the calculation\n",
number1 = float(raw_input("Enter first number: "))
number2 = float(raw_input("Enter second number: "))
operation = raw_input("Enter operation '+' or '-' or '/' or '%' or '*': ")

if operation == '+':
print "= %f" %(number1 + number2)
elif operation == '-':
print "= %f" %(number1 - number2)
elif operation == '*':
print "= %f" %(number1 * number2);
elif operation == '/':
if(number2 == 0):
print "Division by zero error!"
else:
print "= %f\n" %(number1 / number2)
elif operation == '%':
if(number2 == 0):
print "Division by zero error!"
else:
print "= %f\n" %(number1 % number2)
else:
print "Illegal operation!"
Enter the calculation
Enter first number: 25
Enter second number: 24
Enter operation '+' or '-' or '/' or '%' or '*': *
= 600.000000