Skip to content
Advertisement

Think Python 2nd Edition Exercise 7-1

“Square Roots” loop:

JavaScript

Copy the loop from “Square Roots” and encapsulate it in a function called mysqrt that takes a as a parameter, chooses a reasonable value of x, and returns an estimate of the square root of a. To test it, write a function named test_square_root that prints a table like this:

JavaScript

Here’s what I wrote:

JavaScript

Here’s what I got:

JavaScript

As you can see, the array is not neatly aligned. How can I print an array if the elements do not have the same length? Thank you!

Advertisement

Answer

Adjust the print statement to print each element to a fixed number of decimal places using {0:.8f} {0:.10f} and {0:.4f} respectively, or your desired coloumn width.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement