Chapter 13: The Preprocessor and Debugging

Program 13.2, page no. 576

In [2]:
y = 5
for x in range(20):
    print "x = %d y = %d" %(x, y)
    assert(x < y)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-2-e6459a3dedb4> in <module>()
      2 for x in range(20):
      3     print "x = %d y = %d" %(x, y)
----> 4     assert(x < y)

AssertionError: 
x = 0 y = 5
x = 1 y = 5
x = 2 y = 5
x = 3 y = 5
x = 4 y = 5
x = 5 y = 5

Program 13.3, page no. 578

In [2]:
import time
import datetime
import math

calendar_start = datetime.datetime.now()
cpu_start = time.time()
count = 0
iterations = 1000000
answer = 'y'
x = 0.0
print "Initial clock time = ", cpu_start, " Initial calendar time = ", calendar_start
while(answer.lower() == 'y'):
    for i in range(iterations):
        x = math.sqrt(3.14159265)
    count += 1
    print "%d square roots completed.\n" %(iterations*count)
    print "Do you want to run some more(y or n)? "
    answer = raw_input()

cpu_end = time.time()
calendar_end = datetime.datetime.now()


print "Final clock time = ", cpu_end, " Final calendar time = ", calendar_end
print "CPU time for %d iterations is %.2f seconds" %(count*iterations, (float(cpu_end-cpu_start)/time.clock()))
print "Elapsed calendar time to execute the program is", calendar_end-calendar_start, " seconds"
Initial clock time =  1387828819.12  Initial calendar time =  2013-12-24 01:30:19.115309
1000000 square roots completed.

Do you want to run some more(y or n)? 
y
2000000 square roots completed.

Do you want to run some more(y or n)? 
n
Final clock time =  1387828824.23  Final calendar time =  2013-12-24 01:30:24.232139
CPU time for 2000000 iterations is 4.49 seconds
Elapsed calendar time to execute the program is 0:00:05.116830  seconds

Program 13.4, page no. 583

In [3]:
from datetime import *
import time

day = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
month = ["January", "February", "March", "April", "May", "June", "July", "August",
         "September", "October", "November", "December"]
         
now = datetime.now()
suffix = ""

today = day[datetime.today().weekday()]
curr_month = month[now.month-1]

if now.day == 1 or now.day == 21 or now.day == 31:
    suffix= "st"
elif now.day == 2 or now.day == 22:
    suffix= "nd"
elif now.day == 3 or now.day == 23:
    suffix = "rd"
else:
    suffix= "th"
    
print "Today is ", today, " the ",str(now.day)+suffix, curr_month, str(now.year)+".", "The time is ", time.strftime("%H:%M:%S")
Today is  Tuesday  the  24th December 2013. The time is  01:30:43

Program 13.5, page no. 586

In [4]:
from datetime import *
import time

day = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
month = ["January", "February", "March", "April", "May", "June", "July", "August",
         "September", "October", "November", "December"]
         
now = datetime.now()
suffix = ""

curr_month = month[now.month-1]

print "Enter a name: ",
name = raw_input()
print "Enter a DOB for ", name
dob_day = int(raw_input("Day: "))
dob_month = int(raw_input("Month: "))
dob_year = int(raw_input("Year: "))
dob_date = date(dob_year, dob_month, dob_day)

if dob_day == 1 or dob_day == 21 or dob_day == 31:
    suffix= "st"
elif dob_day == 2 or dob_day == 22:
    suffix= "nd"
elif dob_day == 3 or dob_day == 23:
    suffix = "rd"
else:
    suffix= "th"

dob_month = month[dob_month-1]
print name, "Was born on ", str(dob_day)+suffix, dob_month, dob_year, "which was a ", day[date.weekday(dob_date)]
Enter a name: Ricky
 Enter a DOB for  Ricky
Day: 5
Month: 8
Year: 1970
Ricky Was born on  5th August 1970 which was a  Wednesday