Chapter 5: Selection Statements

Example broker.c on Page 81

In [1]:
def main():
    commission=0.0 #variable declaration
    value=float(raw_input("Enter value of trade: ")) #accepting the trade value
    
    #calculating commission according to the value of trade
    if(value < 2500.00):
        commission = 30.00 + 0.017 * value
    elif (value < 6250.00):
        commission = 56.00 + 0.0066 * value
    elif (value < 20000.00):
        commission = 76.00 + 0.0034 * value
    elif (value < 50000.00):
        commission = 100.00 + 0.0022 * value
    elif (value < 500000.00):
        commission = 155.00 + 0.0011 * value
    else:
        commission = 255.00 + 0.0009 * value
        
    if (commission < 39.00):
        commission = 39.00
        
    print "Commission: $%0.2f" % commission #printing the value of commission

if  __name__=='__main__':
    main()
Enter value of trade: 7000.00
Commission: $99.80

Example on Page 83

In [1]:
def main():
    #variable declaration
    i=1
    j=2
    
    if(i>j): #k=i>j?i:j
        k=i   
    else:    
        k=j  #k is now 2
    
    if(i>=0): #k=(i>=0?i:0)+j
        k=i+j #k is now 3
    else:
        k=0+j
if  __name__=='__main__':
    main()

Example date.c on Page 89

In [1]:
def main():
    date=raw_input("Enter date (mm/dd/yy): ") #accepting the date in mm/dd/yy format
    date1=date.split("/")
    #day, moth and year separation
    day=int(date1[1])
    month= int(date1[0])
    year=int(date1[2])
    
    #printing day as an ordinal number
    print "Dated this", day,
    if (day==1 or day==21 or day==31):
        print "st",
    elif (day==2 or day==22):
        print "nd",
    elif (day==3 or day==23):
        print "rd",
    else:
        print "th",
    print "day of",
    
    #printing month
    if (month==1):
        print "January",
    elif(month==2):
        print "February",
    elif(month==3):
        print "March",
    elif(month==4):
        print "April",
    elif(month==5):
        print "May",
    elif(month==6):
        print "June",
    elif(month==7):
        print "July",
    elif(month==8):
        print "August",
    elif(month==9):
        print "September",
    elif(month==10):
        print "October",
    elif(month==11):
        print "November",
    elif(month==12):
        print "December",
    
    #printing year
    print ", 20%0.2d." % year
    
if  __name__=='__main__':
    main()
Enter date (mm/dd/yy): 7/19/14
Dated this 19 th day of July , 2014.
In [ ]: