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

Take User Input

00:00 And let’s tackle the last part of the challenge, which is prompting the user. So instead of adding the values for the amount, the rate, and the years inside your source code, you will use the input() function to prompt the user to enter those values.

00:17 Start with the amount, amount = input(), and the prompt is "Enter a principal amount: ". Don’t forget the space at the end to format it nicely in the shell.

00:30 And the next, rate = input(), and then the prompt is "Enter the annual rate: ". And then years = input(), and as a prompt, "Enter the number of years: ".

00:47 So if you run the module now and you enter something, then you might already know that you will run into an error. Let’s try it out. Enter a principal amount of, for example, a hundred works.

01:02 Enter the annual rate works, enter the number of years, and then you run into the error because only then you call the function in your code, and then you get a TypeError that you can only concatenate string and not int to another string.

01:19 As you know, when you call the input() function, you’ll get a string back. So you need to convert amount and rate to floats.

01:27 And years, since the years are always full years, you work with the int() function call there. The input of the user is converted to float for amount,

01:40 float for rate,

01:44 and an integer for years.

01:47 And when you run it and you enter 100, 0.05, and 4, then the values are perfectly rounded to two digits with no errors, and the output is as you wanted it to be, which solves this challenge.

02:04 Perfect. You might have felt that the challenge was a bit daunting at the beginning when I read it to you, but reducing a bigger challenge to smaller tasks and tackling them bit by bit makes this big challenge chewable.

02:19 And performing the actual steps was more straightforward than tackling the big challenge at once. Still, there might have been points where you were a bit confused, and that’s why I added some additional resources in the upcoming and last lesson of this video course where you can investigate some topics a bit further.

Become a Member to join the conversation.