[ wraith46 @ 26.02.2017. 16:29 ] @
Radim po knjizi Python Programming: An Introduction to Computer Science (2nd Edition) by John Zelle.

Ne mogu da se snađem oko 7. zadatka na 43. strani:

Citat:
As an alternative to APR, the interest accrued on an account is often described in terms of a nominal rate and the number of compounding periods. For example, if the interest rate is 3% and the interest is compounded quarterly, the account actually earns 3/4 % interest every 3 months.

Modify the futval.py program to use this method of entering the interest rate. The program should prompt the user for the yearly rate (rate) and the number of times that the interest is compounded each year (periods). To compute the value in ten years, the program will loop 10 * periods times and accrue rate/period interest on each iteration.


Pokušavam to da uradim na sledeći način:

Code:
# futval.py
#   A program to compute the value of an investment
#   carried n years into the future

def main():

    print("This program calculates the future value of a n-year investment.")
    print()

    principal = eval(input("Enter the initial principal: "))
    initial = principal
    rate = eval(input("Enter the interest rate: "))
    years = eval(input("Enter the number of years: "))
    periods = eval(input("Enter the number of times that the interest is compounded each year: "))
    
    for i in range(years * periods):
        principal = rate / periods

    print("The value in " + str(years) + " years is:", principal)
    print("The accumulation in " + str(years) + " years is: " + str(principal - initial))


main()


Malo sam zaobišao instrukcije kada je reč o tačnom broju godina (10) - ostavio sam mogućnost da i za to bude unešen broj godina. Takođe, dodao sam opciju da se na kraju izlista koliko je novca akumulirano. Elem, problem mi pravi konkretna formula - da li neko kompetentniji može da mi pomogne?

Hvala unapred.
[ zema @ 26.02.2017. 16:52 ] @
greska je u for petlji

Code:


for i in range(years * periods):
    principal += rate / periods


[ wraith46 @ 26.02.2017. 17:12 ] @
Mnogo mi ovo "malo"?

[ zema @ 26.02.2017. 17:43 ] @
Code:


principal = principal * (1 + (rate / periods))

[ wraith46 @ 26.02.2017. 17:46 ] @
Bravo :) hvala puno!
[ wraith46 @ 26.02.2017. 17:54 ] @
BTW:

Citat:
Any book telling you to use eval to convert input strings to numbers is incredibly wrong. Don't do that. Use int or float if you want those.


sa StackOverflow-a