Write a Python program that prompts the user to enter a base number and an exponent, and then calculates the power of the base to the exponent. The program should not use the exponentiation operator (**) or the math.pow() function. The program should handle both positive and negative exponents.
Source Code
# Prompt the user to enter the base number and exponent
base = float(input("Enter the base number: "))
exponent = int(input("Enter the exponent: "))
# Initialize the result variable
result = 1
# Calculate the power
if exponent >= 0:
for i in range(exponent):
result *= base
else:
for i in range(exponent,0):
result /= base
# Print the result
print(base, "raised to the power of", exponent, "is:", result)
Output
Sample Output#1
Enter the base number: 1.5
Enter the exponent: 2
1.5 raised to the power of 2 is: 2.25
Sample Output#2
Enter the base number: 5
Enter the exponent: -2
5.0 raised to the power of -2 is: 0.04