74
3 Loops and Branching
3.4 Exercises
Exercise 3.1: A for Loop with Errors
Assume some program has been written for the task of adding all integers i =
1, 2, . . . , 10 and printing the final result:
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sum = Sum + x
print ’sum: ’, sum
a) Identify the errors in the program by just reading the code.
b) Write a new version of the program with errors corrected. Run this program and
confirm that it gives the correct output.
Filename: for_loop_errors.py.
Exercise 3.2: The range Function
Write a slightly different version of the program in Exercise 3.1. Now, the range
function should be used in the for loop header, and only the even numbers from
[2, 10] should be added. Also, the (only) statement within the loop should read sum
= sum + i.
Filename: range_function.py.
Exercise 3.3: A while Loop with Errors
Assume some program has been written for the task of adding all integers i =
1, 2, . . . , 10:
some_number = 0
i = 1
while i < 11
some_number += 1
print some_number
a) Identify the errors in the program by just reading the code.
b) Write a new version of the program with errors corrected. Run this program and
confirm that it gives the correct output.
Filename: while_loop_errors.py.
Exercise 3.4: while Loop Instead of for Loop
Rewrite average_height.py from Sect. 3.1.3, using a while loop instead.
Filename: while_instead_of_for.py.
Exercise 3.5: Compare Integers a and b
Explain briefly, in your own words, what the following program does.
a = int(input(’Give an integer a: ’))
b = int(input(’Give an integer b: ’))
3 Loops and Branching
3.4 Exercises
Exercise 3.1: A for Loop with Errors
Assume some program has been written for the task of adding all integers i =
1, 2, . . . , 10 and printing the final result:
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sum = Sum + x
print ’sum: ’, sum
a) Identify the errors in the program by just reading the code.
b) Write a new version of the program with errors corrected. Run this program and
confirm that it gives the correct output.
Filename: for_loop_errors.py.
Exercise 3.2: The range Function
Write a slightly different version of the program in Exercise 3.1. Now, the range
function should be used in the for loop header, and only the even numbers from
[2, 10] should be added. Also, the (only) statement within the loop should read sum
= sum + i.
Filename: range_function.py.
Exercise 3.3: A while Loop with Errors
Assume some program has been written for the task of adding all integers i =
1, 2, . . . , 10:
some_number = 0
i = 1
while i < 11
some_number += 1
print some_number
a) Identify the errors in the program by just reading the code.
b) Write a new version of the program with errors corrected. Run this program and
confirm that it gives the correct output.
Filename: while_loop_errors.py.
Exercise 3.4: while Loop Instead of for Loop
Rewrite average_height.py from Sect. 3.1.3, using a while loop instead.
Filename: while_instead_of_for.py.
Exercise 3.5: Compare Integers a and b
Explain briefly, in your own words, what the following program does.
a = int(input(’Give an integer a: ’))
b = int(input(’Give an integer b: ’))
