Introduction
In this project, you will learn how to calculate the square root of an integer and format the output to a specific width and style.
👀 Preview
## Example 1:
Input: 10
Output: +++++++++++++++++++++++++3.162
## Example 2:
Input: 200
Output: ++++++++++++++++++++++++14.142
🎯 Tasks
In this project, you will learn:
- How to implement the
format_square_rootfunction to calculate the square root and format the output - How to accept user input and print the formatted square root
🏆 Achievements
After completing this project, you will be able to:
- Write a program that calculates the square root of an integer and formats the output
- Understand how to use the
math.sqrt()function to calculate the square root - Practice formatting output using f-strings and string manipulation
Implement the format_square_root Function
In this step, you will learn how to implement the format_square_root function to calculate the square root of an integer and format the output.
- Open the
SquareRoot.pyfile in your code editor. - In the
format_square_rootfunction, you need to perform the following tasks:- Calculate the square root of the input number using the
math.sqrt()function. - Format the square root to three decimal places using the
:.3fformat specifier. - Calculate the number of padding spaces needed to make the output 30 characters wide, using the
max()function to ensure the padding length is at least 0. - Create the final output string by adding the padding of plus signs (
+) to the formatted square root.
- Calculate the square root of the input number using the
Here's the completed format_square_root function:
def format_square_root(num):
square_root = math.sqrt(num) ## Calculate the square root of the input number
formatted_output = f"{square_root:.3f}" ## Format the square root to three decimal places
padding_length = max(30 - len(formatted_output), 0) ## Calculate the number of padding spaces needed
output = "+" * padding_length + formatted_output ## Create the final output string with padding
return output
Accept User Input and Print the Formatted Square Root
In this step, you will learn how to accept user input and print the formatted square root.
- In the
if __name__ == "__main__":block, add the following code:- Accept an integer input from the user using the
input()function and convert it to an integer using theint()function. - Call the
format_square_rootfunction with the user input and store the result in theresultvariable. - Print the formatted result using the
print()function.
- Accept an integer input from the user using the
Here's the completed code:
if __name__ == "__main__":
num = int(input("Input: ")) ## Accept an integer input from the user
result = format_square_root(num) ## Format the square root of the input
print("Output: ", result) ## Print the formatted result
Test the Program
Save the
SquareRoot.pyfile.Run the program using the following command:
python SquareRoot.pyWhen prompted, enter an integer value, such as
10or200.Observe the output, which should be formatted as specified in the challenge requirements.
Example output for input
10:Output: +++++++++++++++++++++++++3.162Example output for input
200:Output: ++++++++++++++++++++++++14.142
Congratulations! You have completed the "Square Root Formatting" project. If you have any questions or need further assistance, feel free to ask.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



