Starting from:

$24

Computer Science 222 Laboratory 2 – Practice Using Loops and Doing Arithmetic

Learning Objectives:
Think about the human users of your programs (documentation). Develop simple Python programs that do input, produce output and do arithmetic. Directed Activities: 1. Clear a space on your desk and write the values of the variables as the code executes and the outputs that will be generated from the following code segments. a. for i in range(8): print i i = 0 output = 0 i = 1 output = 1 i = 2 output = 2 i = 3 output = 3 i = 4 output = 4 i = 5 output = 5 i = 6 output = 6 i = 7 output = 7 b. for i in [1, 3, 9]: print i i = 1 output = 1 i = 3 output = 3 i = 9 output = 9 c. for i in range(-1): print i nothing d. sum = 0 for i in range(5): sum = sum + i print sum i = 0 output = 1 i = 1 output = 2 i = 2 output = 3 i = 3 output = 4 i = 4 output = 5 e. power = 2 for i in [2, 4, 6]: print i ** power i = 2 output = 4 i = 4 output = 16 i = 6 output = 36 f. difference = 0 for i in [1, 8, -2, 15, 0]: difference = difference – i print difference i = 1 output = -1 i = 8 output = -9 i = -2 output = -7 i = 15 output = -22 i = 0 output = -22 g. product = 1 for i in range(4): product = product * i print product i = 0 output = 0 i = 1 output = 0 i = 2 output = 0 i = 3 output = 0 h. product = 1 for i in [1, 3, 5]: product = product * i print product i = 1 output = 1 i = 3 output = 3 i = 5 output = 15 2. Log on to the computer using your Cougars account information. Open your WebCT account for this course (labeled CSCI 222-L0#). After completing each of the following exercises, demonstrate your code for the instructor. 3. Create a Python test file. Create a Python program called loopPractice.py. This program should contain the code for the eight loops above. The program should print in the format: Loop A: <the output from a above Loop B: <the output from b above <etc. Make sure you develop the code incrementally. Write the code to print “Loop A:” and then add the code from a above. Once this is working move on to “Loop B”. 4. Check your work. Compare your pencil and paper results with the results generated by the computer. For any mistakes that you made, talk with the instructor, your neighbor, or yourself J to understand why you were incorrect. 5. Add user documentation and iteration to an existing program. Download "convert.py" from WebCT. Modify "convert.py" with a loop so that it executes 5 times before quitting (i.e., it converts 5 temperatures in a row). A user-friendly program should print an introduction that tells the user what the program does. Modify "convert.py" to print an introduction. 6. Use iteration to create a table of data and store into a new program. Modify "convert.py" with a loop so that it computes and prints a table of Celsius temperatures and the Fahrenheit equivalents every 10 degrees inclusive from 0C to 100C. Save it under "convertTable.py". (Note: The user will no longer need to input a value.) 7. Compute a square root. (Adapted from the text, p. 74, Exercise 15) Write a program, newton.py, that approximates the square root of a number using Sir Isaac Newton's method. The method involves refining the approximation of the square root through iteration. The initial approximation of the square root of a number, x, is x/2. Given an approximation, approxi, the following equation describes how to get a new approximation, approxi+1 – Your program should prompt the user to enter a number, x. It should then ask for the number of times to refine the approximation of the square root of x. Print out each approximation. 8. Upload the files to WebCT under the Assignments section: loopPractice.py _______ convert.py ________ convertTable.py ________ newton.py ________ 9. If you left any file on the desktop, remove them. Log off of your computer.

More products