mersenneforum.org Help w/ python.
 Register FAQ Search Today's Posts Mark Forums Read

 2005-10-29, 02:41 #1 a216vcti   Oct 2005 102 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 multi-leg 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 degree-days 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 degree-days. If the temperature is above 80, the amount over 80 is added to the cooling degree-days. Write a program that accepts a sequence of average daily temps and computes the running total of cooling and heating degree-days. 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.
 2005-10-29, 04:12 #2 marc     Jun 2004 UK 139 Posts I love python. What do you need help with?
2005-10-29, 04:29   #3
a216vcti

Oct 2005

102 Posts

Quote:
 Originally Posted by marc I love python. What do you need help with?
here's what I have for the first two questions.
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():
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 2005-10-29 at 04:35

 2005-10-29, 15:22 #5 JHagerson     May 2005 Naperville, IL, USA 110001002 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() The last line in the while loop puts the program back where it started. Try "i = nb" and see what happens. You also might want to change the prompt (or better yet, put in code to test) that the interest rate is a decimal (0.06 rather than 6, for example). I guess if I were doing it, I would use "b" or "balance" rather than "i" which I think you are using as "initial" but could also be confused with "interest." I take it that "nb" is "new balance." Single letter variable names will get the job done but longer ones can make the code more understandable.
 2005-10-30, 00:00 #6 cheesehead     "Richard B. Woods" Aug 2002 Wisconsin USA 22·3·641 Posts I was trying to teach a general method instead of doing the homework for a216vcti. :-)
 2005-10-30, 00:27 #7 marc     Jun 2004 UK 8B16 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))
 2005-10-30, 00:37 #8 JHagerson     May 2005 Naperville, IL, USA 22·72 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.

 Similar Threads Thread Thread Starter Forum Replies Last Post WraithX GMP-ECM 115 2021-08-30 22:54 kelzo Programming 3 2016-11-27 05:16 daxmick Programming 2 2014-02-10 01:45 Xyzzy Programming 20 2009-09-08 15:51 yqiang GMP-ECM 2 2007-04-22 00:14

All times are UTC. The time now is 09:21.

Sun May 29 09:21:38 UTC 2022 up 45 days, 7:22, 0 users, load averages: 1.18, 1.25, 1.41