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

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.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like