Compute the power of a number
def power(base, exponent):
result = 1 for _ in range(exponent): result *= base return result base = float(input("Enter the base number: ")) exponent = int(input("Enter the exponent: ")) result = power(base, exponent) print("Result:", result) In this program, we define a function called `power` that takes a base and exponent as parameters. It then uses a for loop to repeatedly multiply the base by itself `exponent` times, storing the result in the `result` variable.We then prompt the user to enter the base and exponent, convert them to the appropriate data types, and call the `power` function to calculate the result. Finally, we print out the result.
Comments
Post a Comment