Python compound interest calculator 💵

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
why hello everybody in today's video we'll be creating a compound interest calculator in Python for those that don't know interest is a charge for the privilege of borrowing money to some individual or organization when you place money in a bank account you typically accrue interest we'll create a program to tell you what your new balance will be after accruing interest for so many years but the user is going to decide the initial principle that's the investment the rate of interest and the time in years that this balance has been accruing interest so let's begin let's declare three variables a principle a rate that is rate of interest and time time will be in years we've recently learned about while Loops I would like to include those within this program just so we get the hang of using them we will ask the user to type in an initial principal investment we will continue to prompt the user to type in a principle that's above zero our condition will be while principal is less than or equal to zero we will take our principal assign it some user input enter the principal amount then we would like to type cast our input as a floating Point number if our user input principle is less than or equal to zero we need to inform the user principal can't be less than or equal to zero so I'm going to test my program by printing the principle at the end just temporarily enter the principal amount I can't type A negative number and continue what if my investment was negative one thousand dollars principal can't be less than or equal to zero how about zero nope can't do that either is a thousand okay yep a thousand works that is for the principal let's copy this while loop paste it replace principal with rate this is the rate of interest enter the interest rate interest rate can't be less than or equal to zero and let's copy this again paste it change rate to time let's type cast our input as an integer because we're working with whole years enter the time in years time can be less than or equal to zero I'm going to print my principal rate and time we have principal rate time okay we know that principle works enter the interest rate can my interest rate be negative one no it can't can it be zero nope how about 10 10 percent all right that works time can time be zero no it can't can my time be negative one no it can't what about three years all right so we know that our while Loops are working now here's the formula to calculate interest let's say our total that's our total balance equals our principal times 1 plus our rate divided by 100 I'm going to enclose this function with the set of parentheses this portion of our function will take our interest rate which is a whole number then create a decimal enclose this function within the power function raise this function to the power of time and that is how to calculate compound interest then we will print the new balance I'll use an F string balance after our variable time the word years I'll add a placeholder we will add our total then I'll include a dollar sign maybe this will be in dollars but pick any unit of currency you would like I will format this variable with the format specifier we will display two decimal places point to f okay let's try this enter the principal amount I invest one thousand dollars into maybe the stock market the interest rate is maybe ten percent this year the time in years will be one so after one year at 10 interest my new balance is one thousand one hundred dollars let's try it one more time for good measure maybe five hundred dollars with an interest rate of seven over two years your new balance would be five hundred seventy two dollars and forty five cents all right now there is another way of writing this program what if we would like to allow the user to enter in values that are equal to zero while principal is less than zero if principal is less than zero principal can't be less than zero let's do that for rate interest can't be less than zero time time can't be less than zero here's what happens to our program remember that we're declaring our variables at the top uh nothing happens we go straight to the results so the reason that this is happening is that when we reach the while Loops this condition is false from the beginning we never end up entering these while Loops we skip over them because these three conditions are all false we can write a different variation of this while loop where we could say while true true is a Boolean that means this while loop will continue forever unless we explicitly break out of the while loop we're going to add an else clause else we will break break will break out of a loop with our second while loop change rate is less than zero two while true then we will add an else clause else else break out of the loop while true else break out of the loop we should be able to enter zero values in now enter the principal amount zero zero zero balance after zero years is zero dollars this should work the same as before but we should be allowed to enter in zero values one thousand dollars interest rate of zero after one year is still one thousand dollars well okay then everybody I thought that would be an interesting project to create now that we know how while loops work you could write either a standard while loop with a condition such as principal is less than or equal to zero or you could say while true this Loop would continue forever you would need to explicitly break out of the while loop using this break keyword which we'll cover again in four Loops but yeah that is a compound interest calculator in Python
Info
Channel: Bro Code
Views: 38,421
Rating: undefined out of 5
Keywords: python tutorial, python course
Id: aM5dttidg4Q
Channel Id: undefined
Length: 7min 35sec (455 seconds)
Published: Sun Oct 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.