20051029, 02:41  #1 
Oct 2005
2 Posts 
Help w/ python.
can anyone help me? I've got a few programming problem that I can't figure out. here's the problem.
1.) Write a program that uses a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: the amount of the intital investment does not matter: you can use $1. 2.) Write a program that computes the fuel efficiency of a multileg journey. The program will first prompt for the starting odometer reading and then get information about a series of legs. For each leg, the user enters the current odometer reading and the amount of gas used(seperated by a space). The user signals the end of the trip with a blank line. The program should print out the miles per gallon achieved on each leg and the total MPG for the trip. 3.) Heating and cooling degreedays are measures used by utility companies to estimate energy requirements. If the average temperature for a day is below 60, then the number of degrees below 60 is added to the heating degreedays. If the temperature is above 80, the amount over 80 is added to the cooling degreedays. Write a program that accepts a sequence of average daily temps and computes the running total of cooling and heating degreedays. The program should print these two totals after all the data has been processed. If anyone is good at writing programs, I'd appreciate if you could help me out. Thanks. 
20051029, 04:12  #2 
Jun 2004
UK
139 Posts 
I love python. What do you need help with?

20051029, 04:29  #3  
Oct 2005
2 Posts 
Quote:
Code:
#prob. 1. It goes into a neverending loop. I don't know what I'm doing wrong. def main(): i = 100.0 r = input("Please enter the interest rate: ") count = 0 while i<= 200: nb = (i*r) + i count = count + 1 nb = i print "The years are: ", count main() #prob. 2. Somehow it's not giving me the right answers. def main(): stmi = input("Please enter the starting odometer reading: ") tgas = 0.0 if stmi != "": legmi, gas = input("Please enter the leg miles and the gas (leg miles,gas) or press enter for total miles per gallon: ") if legmi != "": dist = legmi  stmi lmpg = dist/gas while legmi != "": tgas = tgas + gas print "The miles per gallon of the leg is ", lmpg stmi = legmi legmi, gas = input("Please enter the leg miles and the gas (leg miles,gas) or press enter for total miles per gallon: ") else: tmpg = (legmi  stmi)/tgas print "The total miles per gallon is ", tmpg main() Last fiddled with by a216vcti on 20051029 at 04:35 

20051029, 07:34  #4 
"Richard B. Woods"
Aug 2002
Wisconsin USA
2^{2}·3·641 Posts 
When you get a neverending loop, look at each program loop and ask yourself:
(1) What, exactly, has to happen in order for this loop to end? If that question has no answer, there's your problem. But if you can answer question (1), then ask: (2) Which program statement or statements can, at some time, cause the event that answers question (1)? If there's no answer to question (2), there's your problem. But if you can answer question (2), then ask: (3) Are you sure that the statement or statements that answer (2) can ever be executed once the loop starts? Last fiddled with by cheesehead on 20051029 at 07:48 
20051029, 15:22  #5 
May 2005
Naperville, IL, USA
2^{2}·7^{2} Posts 
Code:
def main(): i = 100.0 r = input("Please enter the interest rate: ") count = 0 while i<= 200: nb = (i*r) + i count = count + 1 nb = i print "The years are: ", count main() 
20051030, 00:00  #6 
"Richard B. Woods"
Aug 2002
Wisconsin USA
2^{2}·3·641 Posts 
I was trying to teach a general method instead of doing the homework for a216vcti. :)

20051030, 00:27  #7 
Jun 2004
UK
139 Posts 
It doesn't exactly relate to the programming but there's a closed expression for this.
If a is your initial investment then a * r^n is equal to the value of your investment after t years. so ar^n >= 2a when log(a) + log(r^n) >= log(a) + log(2) ie. when n >= log(r)/log(2). If you were given the interest rate of say 5% you'd convert this to r=1.05. math.ceil(math.log(r) / math.log(2)) 
20051030, 00:37  #8 
May 2005
Naperville, IL, USA
304_{8} Posts 
A general suggestion
"Debug data, not code."
During the debugging step, make your code really ugly if necessary with print statements that tell you what the value of each variable is before and after every statement. Put in a code so you can see which print statement dumped out each of the values. The code can be A, B, C or Step 1, Step 2, Step 3, or whatever. Then you can see how the data changes and where your program changes a variable in a way that you don't expect. (Eventually, you can use the watch window in the debugger to see the value of each variable as the program runs, but that is an advanced topic and probably not appropriate for the first lab in your class.) Another thing you can do is to make sure that you don't create unbounded loops. For example, in the first of the cases, instead of just testing only for i less than or equal to 200, you could also test for count less than 200 (for example). That way, even if someone puts in the value zero (or a negative number) for an interest rate, the program will eventually finish. Come to think of it, a test for the input of a string (or zero, or a negative number) as the interest rate and an appropriate error message for that condition would be a good addition to the program. 
Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
A Python Diver for GMPECM...  WraithX  GMPECM  115  20210830 22:54 
Python Coding Help?  kelzo  Programming  3  20161127 05:16 
PHP vs. Python vs. C (all with GMP)  daxmick  Programming  2  20140210 01:45 
Python...  Xyzzy  Programming  20  20090908 15:51 
using libecm from python  yqiang  GMPECM  2  20070422 00:14 