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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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/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/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/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302766{{"`Square Root and Cube Root`"}} python/numeric_types -.-> lab-302766{{"`Square Root and Cube Root`"}} python/type_conversion -.-> lab-302766{{"`Square Root and Cube Root`"}} python/conditional_statements -.-> lab-302766{{"`Square Root and Cube Root`"}} python/dictionaries -.-> lab-302766{{"`Square Root and Cube Root`"}} python/function_definition -.-> lab-302766{{"`Square Root and Cube Root`"}} python/importing_modules -.-> lab-302766{{"`Square Root and Cube Root`"}} python/standard_libraries -.-> lab-302766{{"`Square Root and Cube Root`"}} python/math_random -.-> lab-302766{{"`Square Root and Cube Root`"}} python/python_shell -.-> lab-302766{{"`Square Root and Cube Root`"}} python/build_in_functions -.-> lab-302766{{"`Square Root and Cube Root`"}} end

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.

Summary

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

Other Python Tutorials you may like