Calculating and Formatting Square Roots

PythonPythonBeginner
Practice Now

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_root function 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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/with_statement -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/variables_data_types -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/numeric_types -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/type_conversion -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/conditional_statements -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/tuples -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/dictionaries -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/function_definition -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/importing_modules -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/standard_libraries -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/math_random -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} python/build_in_functions -.-> lab-302768{{"`Calculating and Formatting Square Roots`"}} end

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.

  1. Open the SquareRoot.py file in your code editor.

  2. In the format_square_root function, 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 :.3f format 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.

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.

  1. 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 the int() function.
    • Call the format_square_root function with the user input and store the result in the result variable.
    • Print the formatted result using the print() function.

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

  1. Save the SquareRoot.py file.

  2. Run the program using the following command:

    python SquareRoot.py
  3. When prompted, enter an integer value, such as 10 or 200.

  4. Observe the output, which should be formatted as specified in the challenge requirements.

    Example output for input 10:

    Output:  +++++++++++++++++++++++++3.162

    Example 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.

Other Python Tutorials you may like