Calculate the Exponent (Exercise)
00:00 In this section, you’ll find only one exercise, but it’s going to be a bit more challenging than the previous ones you’ve seen so far. Essentially, you’ll write a program that gets two numbers from the user and raises the first number to the power of the second number.
The program will ask the user to enter the first number, which is the base of the power. Then it’ll ask for another number, the exponent. In this example, the two numbers provided by the user are
3 respectively, so the results should be
1.2 raised to the power of
3, which gives this long number here.
00:53 The reason you’re seeing these repeated nines is an artifact of the representation error of floating-point numbers. It was covered in more detail in the respective Python Basics course, which you may review to refresh your memory if you like.
A very important detail about the
input() function, which is a common source of mistakes, is that it always returns a string, so you’ll need to convert the user’s input into a numeric value before you can do arithmetic on it.
01:41 You can use f-strings to format the resulting message, and you don’t need to worry about validating the user’s input. You can just assume that users are good citizens and only type actual numbers instead of letters or symbols. Okay, I’ll leave you with that.
Become a Member to join the conversation.