Lab 3  Loops

Lab Index  Course Home Page

Task 1, Examine the range function:

You will recall that the templates for the range function which gives a list of integers ased on integer parameters:

As a shorthand for function templates, we will follow the common practice of putting optional parameters in square brackets, so the templates above could be shortened to

In the interpreter try the invoking range with the following arguments and noting the results:

Start Stop Step
1 10 1
1 11 1
50 100 5

range can also be used to count down. Provide an example that counts down from 10 to 1, and test it.

Task 2, Summing the integers from 1 to n

Write a program that will compute the sum 1 + 2 + 3 + … + n.  The user will specify the value of n  You program will need to

Henceforth, use the style of Zelle, with all the code indented under a heading like

def main():

and ending with the (unindented line) that executes your main function:

main()

Task 3, Summing numbers provided by the user:

Write a program to sum a series of numbers entered by the user.  The program should first prompt the user for how many numbers are to be summed.  It should then prompt the user for each number and once at the end (at least in your final version) print the sum with a label.  Outline what your program must do as in task 2.  You can write an outline in the idle editor, first without worrying about comment marks.  Then you can convert your outline to a comment by selecting the lines and then selecting in theFormat Menu, "Comment out lines".   Then convert your outline to a Python program.

Extra credit problems

  1. Modify your Farenheit to Centigrade program of Lab 2 using a loop, so that you can print a table of  Farenheit temperatures and their equivalent Centigrade temperatures every 10 degrees from 0 F to 220 F.  Do not worry about fancy spacing in the table.
  2. The interest problem in the book assumes money is deposited in the account just once at the beginning.  Modify the program so that the account starts with 0, but the same amount of money is deposited at the beginning of each interest adding period.  For instance if $100 is added each year for 3 years and the APR is .10, then this is the calculation by hand for the end of each year:  
    1:  (0 + 100)(1 + .1) = 110  
    2:  (110 + 100)(1 + .1) =  231
    3:  (231 + 100) (1 + .1) = 364.1

    Then the final value printed is 364.1.   Have the user supply the amount added each year, the APR, and the number of years.
If you are able to solve either of these correctly, you can get checked on them hand them in with the other tasks as well, but they are not required.

Get checked off

Try to get check off in person, as described in Lab 2.  

If you need to submit to Blackboard, it is easiest if all programs are in one file.  You can cut and paste your programs (nothing from Task 1) all into one file and modify the function headings so they are different.  They could be main2(), main3(), and maybe extra1() if you did the first extra credit problem....  Then make the lines that execute each program at the end match the names in the definitions at the beginning.  Make the file submitted contain  'lab' and number and at least one user ID run together, like lab3aharrin.py or lab3aharrin-npodder.py.    


Lab Index  Course Home Page