mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2005-10-29, 02:41   #1
a216vcti
 
Oct 2005

2 Posts
Default 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.
a216vcti is offline   Reply With Quote
Old 2005-10-29, 04:12   #2
marc
 
marc's Avatar
 
Jun 2004
UK

139 Posts
Default

I love python. What do you need help with?
marc is offline   Reply With Quote
Old 2005-10-29, 04:29   #3
a216vcti
 
Oct 2005

2 Posts
Default

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():
    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 2005-10-29 at 04:35
a216vcti is offline   Reply With Quote
Old 2005-10-29, 07:34   #4
cheesehead
 
cheesehead's Avatar
 
"Richard B. Woods"
Aug 2002
Wisconsin USA

22·3·641 Posts
Default

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 2005-10-29 at 07:48
cheesehead is offline   Reply With Quote
Old 2005-10-29, 15:22   #5
JHagerson
 
JHagerson's Avatar
 
May 2005
Naperville, IL, USA

110001002 Posts
Default

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.
JHagerson is offline   Reply With Quote
Old 2005-10-30, 00:00   #6
cheesehead
 
cheesehead's Avatar
 
"Richard B. Woods"
Aug 2002
Wisconsin USA

11110000011002 Posts
Default

I was trying to teach a general method instead of doing the homework for a216vcti. :-)
cheesehead is offline   Reply With Quote
Old 2005-10-30, 00:27   #7
marc
 
marc's Avatar
 
Jun 2004
UK

139 Posts
Default

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))
marc is offline   Reply With Quote
Old 2005-10-30, 00:37   #8
JHagerson
 
JHagerson's Avatar
 
May 2005
Naperville, IL, USA

22·72 Posts
Default 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.
JHagerson is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Python Diver for GMP-ECM... WraithX GMP-ECM 114 2020-06-18 12:05
Python Coding Help? kelzo Programming 3 2016-11-27 05:16
PHP vs. Python vs. C (all with GMP) daxmick Programming 2 2014-02-10 01:45
Python... Xyzzy Programming 20 2009-09-08 15:51
using libecm from python yqiang GMP-ECM 2 2007-04-22 00:14

All times are UTC. The time now is 17:23.

Fri Sep 18 17:23:07 UTC 2020 up 8 days, 14:34, 1 user, load averages: 1.58, 1.54, 1.59

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.