Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coding for Economist Minds - Week 1
#-------------------------------------------------------------------
# This is a compound interest calculator, along with some challenge
# Written by Leon Wee, March 2018.
# Anyone may freely copy or modify this program.
#-------------------------------------------------------------------
# C = P[(1+r)^n - 1]
# Where:
# C = the compound interest
# P = the principal investment amount (the initial deposit or loan amount)
# r = the annual interest rate (in decimal instead of percentage)
# n = the number of years
def calculateCompountInterest(P, r, n): # create a function that accepts three input which are P, r and n
C = P*((1+r)**n - 1)
return C #gives C as output
def calculateBalance(C, P): # simple addition function that accept C and P
B = P + C
return B # gives B as output
##this is the challenge data that I gave, your tasks is to calculate the balance of all this data
inputs = [
{
"P": 2750,
"n": 7,
"r": 0.025
},
{
"P": 1980,
"n": 6,
"r": 0.034
},
{
"P": 4637,
"n": 3,
"r": 0.04
},
{
"P": 100,
"n": 100,
"r": 0.034
},
{
"P": 1257,
"n": 6,
"r": 0.048
},
{
"P": 6745,
"n": 7,
"r": 0.012
}
]
for input in inputs:
print(calculateBalance(calculateCompountInterest(input["P"], input["r"], input["n"]), input["P"]))
#----------------------------------------------------------
# This demo program shows the use of for loop statements
# Written by Leon Wee, March 2018.
# Anyone may freely copy or modify this program.
#----------------------------------------------------------
for x in range(10): # print the message for 10 times, by counting from 0,1,2,3,....,8,9
print("I love programming!")
for x in range(10):
if x%2 == 0: # check if the remainder of x divided by 2 is 0, if yes, that means it is indeed an even number
print(str(x) + " is an even number!")
else: # so logically speaking, if x is not even, then it is odd!
print(str(x) + " is an odd number!")
#---------------------------------------------------
# This demo program shows the use of IF statements
# Written by Leon Wee, March 2018.
# Anyone may freely copy or modify this program.
#---------------------------------------------------
weight = eval(input("Please enter your weight:\n")) # accept some input from user
if weight >= 100: # check if the weight is larger than 80
print("Time to get some exercise bruh! >_<")
elif weight >= 50:
print("Great, keep it up! ^_^")
else: # if the weight is less than 50, this part get executed!
print("You probably need to eat more! ._.")
#---------------------------------------------------
# This demo program shows off how elegant Python is!
# Written by Leon Wee, March 2018.
# Anyone may freely copy or modify this program.
#---------------------------------------------------
print("Hello, World!") # This is easy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.