Solution to Practice Problem Set 1 ---------------------------------- 1(a) 0 1 1 0 1 0 1 1(b) 1 0 1 1 0 0 0 1 1(c) Nothing 1(d) Nothing. With n being equal to -50, the program never enters the while-loop. 1(e) We get the following error message: Traceback (most recent call last): File "None", line 1, in ValueError: invalid literal for int() with base 10: 'hello' The error is caused by the fact that the function call to int() expects an argument that can be translated into a valid integer. The string 'hello' clearly cannot. 2(a) 9 7 1 3 2(b) This function outputs the digits of the given non-negative integer in right-to-left order. 3(a) 0 4 16 36 64 3(b) 4 16 36 64 100 4(a) 0 2 4 6 8 4(b) On Wing IDE, the program seems to "hang" forever, until I restart the shell, but if I use python outside of Wing IDE (this is possible to do) I get output runs through the infinite sequence of even numbers starting at 0, until I kill the program. In both cases, the reason for this behavior is that with number equal to 9, the while-loop will only terminate when count equals 9 and since count starts at 0 and increments by 2 in each iteration of the while-loop, it never reaches 9. 5(a) n 20 6 2 0 5(b) n 10 12 14 16 5(c) n m 10 20 11 18 12 16 13 14 14 12 5(d) 10 11 12 6(a) Run-time error; the variable count is used before it is initialized. 6(b) Syntax error; the colon at the end of the while-statement is missing. 6(c) Semantic error; the program is stuck in an infinite loop because count is not incremented inside the while-loop. 6(d) Semantic error; the output starts with "The square of 2 is 4." It is supposed to start with "The square of 1 is 1." 6(e) Semantic error; the output looks like The square of count is 1 The square of count is 4 The square of count is 9 ... Instead of the word "count" actual numbers 1, 2, 3, etc., should appear in the output. Correct program --------------- n = int(raw_input("Enter a number: ")) count = 1 while count <= n: print "The square of", count, "is", count*count count = count + 1