I need to write a code that displays the a return of investments for 0 to 20 years or when the today's value has reached .5 * of the investment initial invested amount. I am stuck with the latter part. When I use an IF statment, I couldn't get any value. Here is my code.
def getExpInvestAmt(investAmt, i, n):
expInvestAmt = int(investAmt * pow(1 + i / 100, n))
return expInvestAmt
def getPresentValue(exp_invest_amt, d, n):
adjAmt = int(exp_invest_amt / pow(1 + d / 100, n))
return adjAmt
def main():
investAmt = float(input("Enter investment amount:"))
i = float(input("Enter the expected investment growth rate:"))
d = float(input("Enter the discount rate:"))
n = 0
adjA = 0
print("Year \t Value at year($) \t Today's worth($)")
for x in range(0, 21):
if adjA < 0.5 * investAmt
break
expected_value = getExpInvestAmt(investAmt, i, n)
present_value = getPresentValue(expected_value, d, n)
n += 1
adjA += 1
print("{} \t {:,} \t {:,}".format(x, expected_value, present_value))
main()
This is what I am suppose to get,
Enter investment amount: 10000
Enter the expected investment growth rate: 5
Enter the discount rate: 6.25
Year Value at Year($) Today's worth($)
1 10,500 9,346
2 11,025 8,734
3 11,576 8,163
4 12,155 7,629
5 12,763 7,130
6 13,401 6,663
7 14,071 6,227
8 14,775 5,820
9 15,513 5,439
10 16,289 5,083
11 17,103 4,751 # today's value have reached <= 50% of the initial amount program stops.
I think you can simplify your code; you don't need adjA or x. Where you are using adjA you should just be using present_value, and you can just iterate n over the range instead of x:
def main():
investAmt = float(input("Enter investment amount:"))
i = float(input("Enter the expected investment growth rate:"))
d = float(input("Enter the discount rate:"))
print("Year \t Value at year($) \t Today's worth($)")
for n in range(1, 21):
expected_value = getExpInvestAmt(investAmt, i, n)
present_value = getPresentValue(expected_value, d, n)
print("{} \t {:,} \t {:,}".format(n, expected_value, present_value))
if present_value < 0.5 * investAmt:
break
To get your expected results for an investAmt of 10000 and i of 5, you need a d value of 12.347528.
Related
I started an online training for Python. The assignment wants to me calculate 45 hours work for 10.50 hourly payment. But, 5 hours in this is overwork. So, the payment goes to 10.50 X 5
So, the payment should be 498.75.
But, my program finds different sum. What is wrong with this code? (Note that I am novice)
hours = float(input("Enter hours: "))
rate = float(input("Enter rate: "))
extraPay = float((hours-40) * (rate*1.5))
def computepay(a, b, c):
paymentCalc= a * b + c
return paymentCalc
x = computepay(hours, rate, extraPay)
print(x)
you need to substract extra hours from basic hours on calculations and also add check that extra_pay will not be in negative. Do, Like this:
hours = float(45)
rate = float(10.5)
extra_hours = float(hours - 40)
extraPay = float(extra_hours * (rate*1.5))
def computepay(a, b, c, d):
if c < 0:
c = 0
payment_calc = (a - d) * b + c
return payment_calc
x = computepay(hours, rate, extraPay, extra_hours)
print(x)
The problem is that you are adding the overpay hours twice, once with the increased rate in extraPay and once in your payment calc (a is still 45).
Also, you need to check if hours are less than 40 because if it is less, extraPay will be negative and your calculation will be wrong.
Here is my suggestion:
def computepay(hours, rate):
bonus = 0
if hours>40:
overtime = hours-40
hours=40
bonus = (overtime)*rate*1.5
return hours*rate+bonus
Try:
hours = float(input("Enter hours: "))
rate = float(input("Enter rate: "))
remainder=(max(0,hours-40))
def computepay(a, b):
paymentCalc= a * b
return paymentCalc
def compute_ext(remainder,rate):
ext = remainder * (rate*1.5)
return ext
base = computepay(min(40,hours), rate)
if remainder:
base+=compute_ext(remainder,rate)
print(base)
you used all the hours instead for a max of 40 to calculate the base pay
I am an absolute beginner in programming (just started yesterday and my major is not CS). I have been struggling with this while loop as I don't know why this print statement is not executed(sometimes it prints as "1"). Please tell me where is wrong. Thanks, guys.
current_saving = 0
# This program intends to calculate the how many months the down
# portion can be paid
annual_input = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost * 0.25
months = 0
while current_saving <= portion_down_payment:
current_saving = annual_input / 12 * portion_saved + current_saving * 0.04 / 12
months = months + 1
print(months)
Modify the code to accumulate savings.
current_saving = 0
# This program intends to calculate the how many months the down
# portion can be paid
annual_input = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost * 0.25
months = 0
while current_saving <= portion_down_payment:
# Accumulate savings
current_saving = current_saving + annual_input / 12 * portion_saved + \
current_saving * 0.04 / 12
print(f'{current_saving=}') # Comment out this line to not print the accumulation
months = months + 1
print(months)
Test Reults
Enter your annual salary:5000
Enter the percent of your salary to save, as a decimal:.5
Enter the cost of your dream home:10000
current_saving=208.33333333333334
current_saving=417.36111111111114
current_saving=627.0856481481482
current_saving=837.5092669753087
current_saving=1048.6342978652262
current_saving=1260.4630788581103
current_saving=1472.9979557876372
current_saving=1686.2412823069292
current_saving=1900.195419914619
current_saving=2114.862737981001
current_saving=2330.245613774271
current_saving=2546.3464324868523
12
Hello my program can count how many customers and tell the customer their price based on the amount of hours but cannot add the total charges to save my life
Here’s my code
count = 1
total_charge = 0
total = 0
hours_parked = 0
ask = 1
standardRate = 2.00
FEE = .50
def calculate_charges(x, f, sr, t):
if x <= 3:
sr = 2.00
t = sr
print(t)
elif x >= 3 and x <= 24:
sr = 2.00
f = (x - 3) * .50
t = f + sr
print(t)
else:
print("We only allow 24 hours tops. Check the number of hours again!")
main(count, total_charge, hours_parked, ask, total)
def main(c, y, h, a, t):
h = int(input("How many hours did the customer park?"))
calculate_charges(h, FEE, standardRate, total)
c += 1
a = int(input("Is there more customers?(Type 1 for YES and 2 for NO.)"))
if a == 1:
y = t + y
main(count, total_charge, hours_parked, ask, total)
else:
y = t + y
c += 1
print("There was a total of ", c, " customers and a profit of $", y, "for the day!" )
main(count, total_charge, hours_parked, ask, total)
How many hours did the customer park?2
2.0
Is there more customers?(Type 1 for YES and 2 for NO.)1
How many hours did the customer park?2
2.0
Is there more customers?(Type 1 for YES and 2 for NO.)2
There was a total of 3 customers and a profit of $ 0 for the day!
Your previous code did not add up the results of the previous calculation.
count = 0
total_charge = 0
total = 0
hours_parked = 0
ask = 1
standardRate = 2.00
FEE = .50
def calculate_charges(x, f, sr):
if x <= 3:
t = sr
print(t)
else:
f = (x - 3) * f
t = f + sr
print(t)
return t
def main(c, y, h, a, t):
while True:
h = abs(int(input("How many hours did the customer park?")))
if h > 24:
print("We only allow 24 hours tops. Check the number of hours again!")
else:
break
t += calculate_charges(h, FEE, standardRate)
c += 1
a = int(input("Is there more customers?(Type 1 for YES and 2 for NO.)"))
if a == 1:
y = t + y
main(c, y, h, ask, total)
else:
y = t + y
print("There was a total of ", c, " customers and a profit of $", y, "for the day!")
main(count, total_charge, hours_parked, ask, total)
How many hours did the customer park?27
We only allow 24 hours tops. Check the number of hours again!
How many hours did the customer park?6
3.5
Is there more customers?(Type 1 for YES and 2 for NO.)1
How many hours did the customer park?28
We only allow 24 hours tops. Check the number of hours again!
How many hours did the customer park?10
5.5
Is there more customers?(Type 1 for YES and 2 for NO.)2
There was a total of 2 customers and a profit of $ 9.0 for the day!
You're calling
main(count, total_charge, hours_parked, ask, total)
but count, total_charge, hours_parked, ask and total are not being updated anywhere.
So you're passing in total_charge = 0 and that's what the program is returning.
Try passing c, y, h, a, t into main within your main function. (btw, it might be simpler to do everything in a while in the calculate_charges function)
Also...
It may be easier for you to figure out what's happening if your variables have meaningful names. After studying CompSci for 4 years in undergrad and now as a graduate student, one important lesson I've learned is that following coding convention will pay off both in the short run and in the long run.
Suppose you currently have $50,000 deposited into a bank account and the account pays you a constant interest rate of 3.5% per year on your deposit. You are planning to buy a house with the current price of $300,000. The price will increase by 1.5% per year. It still requires a minimum down payment of 20% of the house price.
Write a while loop to calculate how many (integer) years you need to wait until you can afford the down payment to buy the house.
m = 50000 #money you have
i = 0.035 #interest rate
h = 300000 #house price
f = 0.015 #amount house will increase by per year
d= 0.2 #percent of down payment on house
y = 0 #number of years
x = 0 #money for the down payment
mn = h*d #amount of down payment
while m <= mn:
m = (m+(m*i)) #money you have plus money you have times interest
y = y + 1 #year plus one
mn = mn +(h*f*y)
print(int(y))
The answer you should get is 10.
I keep getting the wrong answer, but I am not sure what is incorrect.
You can simplify the code by using the compound interest formula.
def compound_interest(amount, rate, years):
return amount * (rate + 1) ** years
while compound_interest(m, i, y) < d * compound_interest(h, f, y):
y += 1
If you are allowed to do without the while loop, you can resolve the inequality after the years y.
So you get this code snippet:
import math
base = (i + 1) / (f + 1)
arg = (d * h) / m
y = math.ceil(math.log(arg, base))
I'm working on this simple task where a financial advisor suggests to invest in a stock fund that is guaranteed to increase by 3 percent over the next five years.
Here's my code:
while True:
investment = float(input('Enter your initial investment: '))
if 1000 <= investment <= 100000:
break
else:
print("Investment must be between $1,000 and $100,000")
#Annual interest rate
apr = 3 / 100
amount = investment
for yr in range(5):
amount = (amount) * (1. + apr)
print('After {:>2d} year{} you have: $ {:>10.2f}'.format(yr, 's,' if yr > 1 else ', ', amount))
You got it. The only problem is that apr is runing integer math. Use floating point numbers instead, so apr does not round to zero:
apr = 3.0 / 100.0
By changing that line your program will probably work
This is the whole code changes (as requested in comments):
while True:
investment = float(input('Enter your initial investment: '))
if 1000 <= investment <= 100000:
break
else:
print("Investment must be between $1,000 and $100,000")
#Annual interest rate
apr = 3.0 / 100.0
amount = investment
for yr in range(5):
amount = (amount) * (1. + apr)
print('After {:>2d} year{} you have: $ {:>10.2f}'.format(yr, 's,' if yr > 1 else ', ', amount))
The output I get is:
Enter your initial investment: 1002
After 0 year, you have: $ 1032.06
After 1 year, you have: $ 1063.02
After 2 years, you have: $ 1094.91
After 3 years, you have: $ 1127.76
After 4 years, you have: $ 1161.59
new_a=1000
yi = .03
for yr in range(1,6):
new_a = new_a+new_a*yi
print('After {:>2d} year{} you have: $ {}'.format(yr, 's,' , new_a))