Created
March 16, 2018 14:39
-
-
Save leon-sleepinglion/f8865e10c5291a3f7a6d748f3e93ae0b to your computer and use it in GitHub Desktop.
Coding for Economist Minds - Week 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------- | |
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------------------------------------------------------- | |
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--------------------------------------------------- | |
# 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--------------------------------------------------- | |
# 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