Starting from:
$35

$29

Laboratory 3 Arithmetic, Loops Solution

Learning Objectives:

 
Develop simple Python programs that do input, produce output and do arithmetic using loops.

 
Develop simple Python programs that use for loops.




Directed Activities:

0. Download lab3.py. Demonstrate each of your solutions for the instructor.




 
Finding an average

Write a function, average(), that asks the user the number of grades to input. The function should then ask for each grade and output the average. The message to the user should be:

Enter your grade on HW1:

Enter your grade on HW2:






 
Fibonacci.

A Fibonacci sequence is a sequence of numbers where each successive number is the sum of the previous two. The classic Fibonacci sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, …. Write a function, fibonacci() , that computes and outputs all of the Fibonacci numbers up to and including the nth number, where n is a value input by the user.




 
Compute a square root. (Adapted from the text, p. 78, Exercise 17)

Write a function newton() that approximates the square root of a number using Sir Isaac Newton's method. Ask the user what the number, x, is and how many times to improve the approximation. x / 2 is a good first approximation. Given an existing approximation,




approx = approx + x

approx













is a better approximation. Display the number and the final value of the approximation.




4. Draw a house

Write a function, buildHouse(), that uses the author’s graphic package to create a blue background with a white square house and a brown triangle roof. The house should be in the middle of the window. Use Rectangle class to build the house. Use Polygon to build the triangle.




5. Output a sequence

Write a function, sequence(), that asks the user for the number of terms in a series, then prints the following pattern for the appropriate number of terms.

 

1 3 3 5 5 7 7 9 9 …




6. Computing pi.

Write a function pi() that approximates the value of using the Wallis formula:

π 2 * 2 * 4 * 4 * 6 * 6 * 8

2 1 3 3 5 5 7 7







The program should prompt the user for n, the number of terms in the series, compute the product of the n terms, and output the resulting approximation of pi.




 
Upload the file to your OAKS account. lab3.py If you left any files on the desktop, remove them. Log off of your computer.

More products