[Tutor] Loop help
Fidel Sanchez-Bueno
fidellira.6 at gmail.com
Mon Aug 17 00:16:05 CEST 2009
Nathan Wing escribió:
> Hello All,
> I've been racking my brain trying to figure out, what I imagine to be,
> a very simple problem. I found the Intro to comp science MIT open
> course
> (http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2007/CourseHome/index.htm)
> and was working on the very first homework assignment. I was
> successful in problem one, but having a hard time with problem two.
> Any help would be greatly appreciated!
>
> Regards,
> Nathan
>
> Here is the problem:
>
> A wealthy donor has given MIT $500,000 to be used to pay the tuition
> of one student every other year until the money runs out. Under the
> assumptions that
> a. The current annual tuition is $34,986 per year,
> b. Tuition goes up at a rate of 4.1%/year, and
> c. The principal will be invested so that it earns 5%/year,
>
> solve the following problems:
> 1) Write a program that calculates and prints the total number of
> students this gift will support and the tuition paid for the last student.
> 2)Write a program that calculates and prints the smallest gift
> (rounded to the nearest $100) needed to support one student every
> other year for 100 years. In addition, print the number of iterations
> your program took to arrive at the answer. Hint: you may want to use
> nested loops for this problem.
>
> my code that works for number 1:
>
> #!/usr/bin/env python
> #
> #scholarship.py
> #
>
> #initialize values
> year = 0
> count = 0
> money = 1000000.0 #fund amount
> ctuition = 34986.0 #current tuition
> rate = 1.041 # 4.1% annual increase on tuition
> principle = 1.05 #5% annual interest accrued on fund
>
> ###calculations
> while money > 0 and money >= ctuition: #while the fund has money and
> can cover tuition
> if year % 2 == 0: #if this is year 0 or every even year after year 0
> money = money - ctuition
> ftuition = ctuition
> count += 1
>
> ctuition = ctuition * rate
> money = money * principle
> year += 1
>
> print "Total number of students aided: %d" % count
> print "Tuition paid for final recipient: $%.2f" % ftuition
> print 'Total number of years fund was operative: %s' % str(year + 1)
>
>
> my code for number two (this code ends up looping and does not
> increment the initial funding, as I had thought it would):
>
> #!/usr/bin/env python
> #
> #scholarship2.py
> #
>
> ### initialize values
>
> loop = 1
> while loop is 1:
>
> year = 0
> count = 0
> initmoney = 500000.0 #initial funding
> money = initmoney #fund amount
> ctuition = 34986.0 #current tuition
> rate = 1.041 # 4.1% annual increase on tuition
> principle = 1.05 #5% annual interest accrued on fund
>
>
> ### calculations
> while money > 0 and money >= ctuition: # while the fund has money
> and can cover tuition
> if year % 2 == 0: # if this is year 0 or every even year after
> year 0
> money = money - ctuition
> ftuition = ctuition
> count += 1
> print "pass ", count
>
> ctuition = ctuition * rate
> money = money * principle
> year += 1
>
> if year == 100:
> loop = 0
> print "The total funding required to last 100 yrs: $%.2f" %
> initmoney
> print count
> print year
> else:
> print 'restart with new funding'
> initmoney += 100.0
> year = 0
> count = 0
> ctuition = 34986.0
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
hi..
i dont know if this is the right anwser but this is how i do it:
for the first question:
**************************************
money = 500000
money_rate = 0.05
tuiton_cost = 34986
tuiton_rate = 0.041
year = 1 # first year
student = 0
while money >= tuiton_cost:
year += 2
money -= tuiton_cost
student += 1
for a in range(2):
money += (money * money_rate)
tuiton_cost += (tuiton_cost * tuiton_rate)
print "Number of student supported is %s" % student
print "Cost of the last tuiton is %s" % tuiton_cost
********************************************
for the second one:
********************************************
cost = 0
money_rate = 0.05
tuiton_cost = 34986
tuiton_rate = 0.041
year = 1 # first year
while year <= 100:
cost += tuiton_cost
year += 2
for a in range(2):
tuiton_cost += (tuiton_cost * tuiton_rate)
cost -= (cost * money_rate)
print "the smallest gift must be %i" % cost
*******************************************
for the second one the number of iteration for the while loop is 50
More information about the Tutor
mailing list