Square Root and Cube Root

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to calculate the square root or cube root of an input number, depending on whether the number is odd or even. This project will help you understand the basic concepts of mathematical operations and data processing in Python.

👀 Preview

Enter a number: 2
>>> 1.26

Enter a number: 36
>>> 3.30

Enter a number: 9
>>> 3.00

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create a Python script that takes user input
  • How to determine whether a number is odd or even
  • How to calculate the square root and cube root of a number using the math module
  • How to round the calculated result to two decimal places

🏆 Achievements

After completing this project, you will be able to:

  • Write a Python script that can calculate the square root or cube root of an input number
  • Understand the basic logic of conditional statements and mathematical operations in Python
  • Apply your knowledge of Python to solve a practical problem

Implement the square_cube() Function

In this step, you will implement the square_cube() function, which will contain the code to calculate the square root or cube root of an input number.

  1. In the square_cube.py file, add the following code:
import math


def square_cube():
    """
    Calculate the square root or cube root of an input number.

    If the input number is odd, calculate its square root and return the result rounded to two decimal places.
    If the input number is even, calculate its cube root and return the result rounded to two decimal places.

    Returns:
        float: The calculated result rounded to two decimal places.
    """
    x = int(input("Enter a number: "))

    if x % 2 == 1:
        result = math.sqrt(x)
    else:
        result = x ** (1 / 3)

    return "{:.2f}".format(result)


if __name__ == "__main__":
    print(square_cube())

This code defines a function called square_cube() that takes an input number x from the user, determines whether the number is odd or even, and then calculates the square root or cube root of the number, respectively. The result is then rounded to two decimal places and returned.

Test the square_cube.py File

In this step, you will test the square_cube.py file to ensure that it is working correctly.

  1. Open a terminal or command prompt and navigate to the directory where you saved the square_cube.py file.
  2. Run the following command to execute the square_cube.py file:
python3 square_cube.py
  1. When prompted, enter a number and observe the output. For example:
Enter a number: 2
>>> 1.26

Enter a number: 36
>>> 3.30

Enter a number: 9
>>> 3.00

The output should match the expected results in the challenge description.

  1. If the output is correct, you have successfully completed the project. If you encounter any issues, review the code in the square_cube.py file and make any necessary corrections.

Congratulations! You have completed the "Square Root and Cube Root" project. If you have any further questions or need additional 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