Calculate the Exponent (Solution)
As you can see, it’s stores a string, which we can double-check by calling the
type() function on the variable. To convert these values into numbers, we can call either the
int() function or the
float() one. In this case, it seems more appropriate to call
float() because we don’t know whether the user will provide a whole number or a fractional number, so it’s better to stay on the safe side. Let’s add the necessary conversion then. Now, since we’re in the numeric domain at this point, we can proceed to calculate the result, which is the
base raised to the power of the
01:49 Notice that each variable that we refer to must be enclosed in curly brackets to replace it with the corresponding value. Otherwise, the string would just print the variable name literally. Okay.
02:01 Let’s give our program a spin. I’ll save it and reload it. I’ll provide the same values as before. Great. We’re done with this exercise, but if you feel like it, you can keep improving your solution—for example, by formatting the resulting number to limit the number of decimal digits.
Become a Member to join the conversation.