The provided Python program is designed to compute the power of a given number by accepting user input for the base and exponent. The core functionality is encapsulated within a function named compute_power, which takes two parameters – the base and the exponent. Within the function, the ** operator is employed for exponentiation, but alternatively, the pow() function could be used. The user is prompted to enter a floating-point value for the base and an integer value for the exponent. After gathering the input, the program invokes the compute_power function, calculates the result, and subsequently displays a formatted output that includes the original base, exponent, and the computed result. This program serves as a simple yet effective tool for calculating powers in a user-friendly manner, providing insight into the fundamental mathematical operation of exponentiation in Python.
Source code
# Taking input from the user
base = float(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
# Computing and displaying the result
result = compute_power(base, exponent)
print(f"{base} raised to the power {exponent} is equal to {result}")
Description
Certainly! Let’s break down the provided Python program step by step:
- Function Definition:
def compute_power(base, exponent):
result = base ** exponent
return result
Here, a function named compute_power is defined, which takes two parameters (base and exponent). Inside the function, the ** operator is used for exponentiation, and the result is stored in the variable result. Finally, the function returns the computed result.
- User Input:
base = float(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
The program prompts the user to enter the base and exponent. The input function is used to capture user input as strings, and float() and int() functions are used for converting them to appropriate data types.
- Function Invocation:
result = compute_power(base, exponent)
The compute_power function is called with the user-provided values for the base and exponent, and the result is stored in the variable result.
- Output:
print(f"{base} raised to the power {exponent} is equal to {result}")
The program prints a formatted string that includes the original base, exponent, and the computed result. The f-string allows for easy incorporation of variables into the output string.
- Example Execution:
Enter the base: 2.5
Enter the exponent: 3
2.5 raised to the power 3 is equal to 15.625
In this example, the user inputs a base of 2.5 and an exponent of 3. The program then calculates and displays the result, which is 15.625.
Overall, this Python program provides a straightforward way for users to compute the power of a number by leveraging a dedicated function and user-friendly input prompts.
Screenshot

