# The following is to organize the output : # For the actual questions and answers jump down to the main block below. prob_no = 0 # Global variable to keep track of problem numbers def sep() : separator = "" for i in range(75) : separator += "-" print separator def pause() : raw_input("To continue, press enter ... ") # To print the question : def question(text) : global prob_no prob_no += 1 print pause() print sep() print "Problem", prob_no, ":" print text sep() print # To print the answer : def answer(text) : global prob_no print pause() sep() print "Answer", prob_no, ":" print text sep() print # To print the output def output(text) : global prob_no print pause() sep() print "Output for problem", prob_no, ":" exec(text) sep() print ################################################################# # Main block # ################################################################# # Now the questions q1 = """ Ask the user for a positive number n. If n <= 0 print an error saying the number is expected to be positive. Otherwise, print all odd numbers >= 1 but < n using a while loop. """ q2 = """ Modify the previous problem to not only generate the odd numbers, but store them in a list. Then print the list. Do not use range. """ q3 = """ Input a list. Replace the entries of the list with their squares. Write this code using the for statement. (You can use while, but use for too.) """ q4 = """ Make a table of sines starting from 0 degress to 90 degrees, with a step of 2.5 degrees. (In otherwise, the table should list sin(0), sin(2.5), sin(5) etc up to sin(90).) """ q5 = """ Input N. Find the smallest n such that 1 + 1/2 + 1/3 + ... + 1/n > N. Test the function for N < 10. What happens when you input, say N = 20? """ q6 = """ x = 1 y = 2 z = "a" u = "b" First guess and then if the following expressions are true or false: 1 >= 2 x > 4 x != 5 x == 2 and z == "a" or y == 2 x == 2 and (z == "a" or y == 2) (x == 2 and z == "a") or y == 2 not x == 2 and y == 1 1/947.0 * 947 == 1 """ # Try to give an explanation for the last statement above. q7 = """ Given a starting point, a, an end point, b, and some positive integer N, write a code to return N+1 numbers equispaced between a and b, inclusive of a and b. """ # Answers a1 = """ n = input("Input a positive integer : ") if n <= 0 : print "Expected a positive integer." else : i = 1 while i < n : print i, i += 2 print """ a2 = """ n = input("Input a positive integer : ") if n <= 0 : print "Expected a positive integer." else : lst = [] i = 1 while i < n : lst.append(i) i += 2 print lst """ a3 = """ a3_lst = input("A list of numbers : ") for pos, no in enumerate(a3_lst) : a3_lst[pos] = no * no print a3_lst """ a4 = """ import math print "\tdegrees sin" for d in range(0, 361, 10) : d = float(d)/4 print "\t%7.1f %8.5f" % (d, math.sin(math.pi/180 * d)) """ a5 = """ N = input("A positive number greater than 1 : ") if N < 1 : print 1 else : sum = 1.0 k = 1 while sum <= N : k += 1 sum += 1.0/k print k """ a6 = """ x = 1 y = 2 z = "a" u = "b" if 1 >= 2 : print "1 >= 2 :", True else : print "1 >= 2 :", False if x > 4 : print "x > 4 :", True else : print "x > 4 :", False if x != 5 : print "x != 5 :", True else : print "x != 5 :", False if x == 2 and z == "a" or y == 2 : print 'x == 2 and z == "a" or y == 2 :', True else : print 'x == 2 and z == "a" or y == 2 :', False if x == 2 and (z == "a" or y == 2) : print 'x == 2 and (z == "a" or y == 2) :', True else : print 'x == 2 and (z == "a" or y == 2) :', False if (x == 2 and z == "a") or y == 2 : print '(x == 2 and z == "a") or y == 2 :', True else : print '(x == 2 and z == "a") or y == 2 :', False if not x == 2 and y == 1 : print "not x == 2 and y == 1 :", True else : print "not x == 2 and y == 1 :", False if 1/947.0 * 947 == 1 : print "1/947.0 * 947 == 1 :", True else : print "1/947.0 * 947 == 1 :", False """ a7 = """ a = input("Starting point : ") b = input("Ending point : ") N = int(input("No.of division : ")) # While loop implementation a7_lst1 = [] point = a jump = float(b-a)/N # Length of each interval while point < b : a7_lst1.append(point) point += jump a7_lst1.append(b) # If you assume some restrictions on a, b, say for example they are # specified upto two decimal spaces. You can also do a7_lst2 = [] a_scaled = int(100*a*N) b_scaled = int(100*b*N) jump = int(100*(b-a)) for scaled_point in range(a_scaled, b_scaled, jump) : a7_lst2.append(float(scaled_point)/(100*N)) a7_lst2.append(b) print "While loop implementation :", a7_lst1 print "For loop implementation with range :", a7_lst2 """ # Now the output. Don't worry about the folowing code either. question(q1) answer(a1) output(a1) question(q2) answer(a2) output(a2) question(q3) answer(a3) output(a3) question(q4) answer(a4) output(a4) question(q5) answer(a5) output(a5) question(q6) answer(a6) output(a6) question(q7) answer(a7) output(a7)