Re: [Tutor] Loop help
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 = 100.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 = 50.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@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 = 50 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop help
Nathan Wing wrote: 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 = 100.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 = 50.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 This isn't your only problem, but the immediate difficulty you mention, the incorrect value for initmoney, is caused by initializing said value inside the while loop. Thus every time you go through the while loop, it will be set to the same value. Since you want to increment it by 100, you need to move the initialization outside of the loop: initmoney = 50.0 #initial funding loop = 1 while loop == 1: year = 0 count = 0 money = initmoney #fund amount ... I think I'd also eliminate the loop variable in favor of a "break" statement. Make it a while True, and use break when you print out the result. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Loop help
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 = 100.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 = 50.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@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating through objects
"GoodPotatoes" wrote print page1.readlines() From this excercise, the first time I read page1 I get all of the lines. The best thing to do is store them in a variable then print them. That way you can access them at will: lines = page1.readlines() print lines # all of them as a list print lines[2],lines[5], lines [-1] # 3rd, 6th and last as lines HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating through objects
GoodPotatoes wrote: I'm not sure if I've been searching using the correct terms. After I've iterated through an object, such as a cursor or webpage, how do I get back to the "top"? e.g. tutorial from http://www.daniweb.com/code/snippet563.html# print page1.readlines() Capture the lines in a variable. foo = page1.readlines() print foo From this excercise, the first time I read page1 I get all of the lines. The next time, I only get []. What happens to all of the info after I've read it? Do I need to again get the info from the web? page1 = opener1.open(picture_page) * * * * ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Iterating through objects
I'm not sure if I've been searching using the correct terms. After I've iterated through an object, such as a cursor or webpage, how do I get back to the "top"? e.g. tutorial from http://www.daniweb.com/code/snippet563.html# print page1.readlines() >From this excercise, the first time I read page1 I get all of the lines. The >next time, I only get []. What happens to all of the info after I've read it? Do I need to again get the info from the web? page1 = opener1.open(picture_page) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python's database
I don't know how much it's in use, but I thought gadfly ( http://gadfly.sourceforge.net/) was the db that's implemented in python. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor