[Tutor] Loop help
Nathan Wing
nathan.wing at gmail.com
Sun Aug 16 23:35:00 CEST 2009
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090816/9c01940a/attachment-0001.htm>
More information about the Tutor
mailing list