Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

00:17 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 1.2 and 3 respectively, so the results should be 1.2 raised to the power of 3, which gives this long number here.

00:40 This is actually a rational number with a fixed number of decimal digits. If you were to punch these numbers into a calculator, the result would be exact and equal to 1.728.

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.

01:08 Before you get started, here are a bunch of useful tips for you. First of all, you can call the built-in input() function to ask the user for data.

01:17 Whatever this function returns, you can assign it to a helper variable, which you can then use in subsequent computations.

01:25 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.

01:58 Remember to break down the problem into smaller, manageable steps, and then we’ll go over this exercise together in the next lesson.

Become a Member to join the conversation.