Chapter 6: Making decisions

Program 6.1, Page number: 66

In [1]:
number=12                #number=int(raw_input("Type in your number: "))

try:
    if( number < 0 ):
      number=-number     #change sign,if number is negative
except: 
    print "not a number" #Invalid input/value error
 
print ("The absolute Value is {0}".format(number))
The absolute Value is 12

Program 6.2, Page number: 67

In [7]:
gradeTotal=0
failureCount=0
i=0

numberOfGrades=5                                     #numberOfGrades=int(raw_input("How many grades will you be entering? "))
grade=[72,83,91,89,95]

while(i<numberOfGrades):
     gradeTotal=gradeTotal+grade[i]
     if(grade<65):
         failureCount=failureCount+1;
     i=i+1

average=float(gradeTotal/numberOfGrades)
      
print("Grade average= {0:.2f}".format(average))
print("Number of failures= {0}".format(failureCount))
Grade average= 86.00
Number of failures= 0

Program 6.3, Page number: 70

In [8]:
number_to_test=30                     #number_to_test=int(raw_input("Enter your number to be tested: "))

remainder=number_to_test%2

if(remainder==0):
   print("The number is even")
if(remainder!=0):
   print("The number is odd")
The number is even

Program 6.4, Page number: 71

In [9]:
number_to_test=45                            #number_to_test=int(raw_input("Enter your Number to be tested: "))

remainder=number_to_test%2

if(remainder==0):
    print("The number is even.\n")
else:                      #using 'else' instead of second 'if'
    print("The number is odd.\n")
The number is odd.

Program 6.5, Page number: 73

In [10]:
year=1900                               #year=int(raw_input("Enter the year to be tested: "))

rem_4=year%4
rem_100=year%100
rem_400=year%400

if((rem_4==0 and rem_100!=0)or rem_400==0):
    print("It's a leap year.")
else:
    print("Nope, it's not a leap year.\n")
Nope, it's not a leap year.

Program 6.6, Page number: 77

In [11]:
number=450                                 #number=int(raw_input("Please type in a number: "))

try:
   if(number<0):         #Negative Number
     sign=-1
   elif(number==0):      #No sign
     sign=0
   else:                 #Positive Number
     sign=1
except:                  #Value error
   print("invalid input")

print("Sign= {0}".format(sign))
Sign= 1

Program 6.7, Page number: 78

In [13]:
ch='z'                                    #ch=raw_input("Enter a single character: ")

if((ch>='a' and ch <='z') or (ch>='A' and ch<='Z')):
     print("It's an alphabetic character.")
elif(ch>='0'and ch<='9'):
     print("It's a digit.")
else:
     print("It's a special character")
It's an alphabetic character.

Program 6.8, Page number: 80

In [1]:
try:
  print("Type in your expression(with spaces inbetween): ")
  value1, operator, value2="5 + 2".split()
  #value1, operator, value2=raw_input().split()

except:
  print("err.. follow the syntax <value operator value>") 
  print("with spaces inbetween\n")

value1,value2=[float(value1),float(value2)]

if(operator=='+'):
     print("answer: {0:.2f}".format(value1+value2))
elif(operator=='-'):
     print("answer: {0:.2f}".format(value1-value2))
elif(operator=='*'):
     print("answer: {0:.2f}".format(value1*value2))
else:
     print("answer: {0:.2f}".format(value1/value2))
Type in your expression(with spaces inbetween): 
answer: 7.00

Program 6.9, Page number: 85

In [2]:
try:
   print("Type in your expression(with spaces inbetween): ")
   value1, operator, value2="3 * 5".split()
   #value1, operator, value2=raw_input().split()
 
    
except:
  print("err.. follow the syntax <value operator value>") 
  print("with spaces inbetween\n")

value1,value2=[float(value1), float(value2)]

if(operator=='+'):
   print("Answer= {0:.2f}".format(value1+value2))
elif(operator=='-'):
   print("Answer= {0:.2f}".format(value1-value2))
elif(operator=='*'):
   print("Answer= {0:.2f}".format(value1*value2))
elif(operator=='/'):
   if(value2==0):
      print("Whoops! divide by 0 issue")
   else:
      print("Answer= {0:.2f}".format(value1/value2))
else:
   print("err.. Invalid operator")
Type in your expression(with spaces inbetween): 
Answer= 15.00

Program 6.10, Page number: 87

In [14]:
p=2

while(p<=50):          #Outer loop
   isPrime=1           #Variable declaration
   d=2                 
   while(d<p):         #Inner loop
       if(p%d==0):
         isPrime=0
       d=d+1           #End of inner loop
      
   if( isPrime!=0):
      print " ",p    
   p=p+1               #End of outer loop
  2
  3
  5
  7
  11
  13
  17
  19
  23
  29
  31
  37
  41
  43
  47