mersenneforum.org Python Coding Help?
 Register FAQ Search Today's Posts Mark Forums Read

 2016-11-09, 01:51 #1 kelzo   Nov 2016 1 Posts Python Coding Help? I have a problem. I don't know how to get input from users and calculate in the way I need to. Here is my prompt for the code.. 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. For this exercise, the program should get input from the Penn State Climatologist website. Start with this code, which prints average temperatures recorded at the Harrisburg airport over the past year. from urllib.request import Request, urlopen def getAverageTemps(startDate, endDate): url = "http://climate.psu.edu/data/ida/submit.php" data = ("siteid=KMDT" + # KMDT is Harrisburg International Airport. "&datastart=" + startDate + "&dataend=" + endDate + "&db=faa_daily" + "&choices[]=date" + "&choices[]=temp_avg" + "&filetype=file" + "&isMeta=1") dataBytes = data.encode() request = Request(url, dataBytes) response = urlopen(request).read().decode() lines = response.split("\n") temps = [] for line in lines: if line[0:4].isnumeric(): temp = line.split(",")[1] temps.append(float(temp)) return temps def main(): print(getAverageTemps("2013-11-01", "2014-10-31")) if __name__ == "__main__": main() (Write your program so that it inputs start and end dates of the form M/D/Y and converts them to the form required by the Penn State Climatologist site) Here is a sample output. This program computes the heating and cooling degree-days for Harrisburg, PA, over a period of time, using data from the Penn State Climatologist website. Start date (e.g., 1/1/2013)? 11/1/2013 End date (e.g., 12/31/2013)? 10/31/2014 Heating degree-days: 4510.13 Cooling degree-days: 13.64
 2016-11-09, 04:34 #2 LaurV Romulan Interpreter     Jun 2011 Thailand 25×32×31 Posts We have some python gurus here which may help you (that is not me!) but first an advice, please use "code" tags when you paste code, especially for languages which are space-sensitive. Otherwise the code is a mess and difficult to read, as in the normal text (non code) the spaces at the beginning of the line are compressed. Second, this is not factoring related, it should be moved in the "homework help" thread, or so.
2016-11-09, 05:59   #3
WraithX

Mar 2006

23·59 Posts

Quote:
 Originally Posted by kelzo I have a problem. I don't know how to get input from users and calculate in the way I need to. Here is my prompt for the code.. Here is a sample output. This program computes the heating and cooling degree-days for Harrisburg, PA, over a period of time, using data from the Penn State Climatologist website. Start date (e.g., 1/1/2013)? 11/1/2013 End date (e.g., 12/31/2013)? 10/31/2014 Heating degree-days: 4510.13 Cooling degree-days: 13.64
First, like LaurV said, using "code" tags will preserve spacing in the code you paste here and will make it easily readable.

An example will look like this:
Code:
def main():
print(getAverageTemps("2013-11-01", "2014-10-31"))

if __name__ == "__main__":
main()
You can either put the "code" tags in manually ["CODE"][/"CODE"] (without the double-quotes), or click on the "#" symbol in the option bar while composing your post. Then, paste your code in-between the tags and it will show up formatted nicely.

Second, are you learning this material in a class? Have you covered user input yet? Are you allowed to use Google to find what functions are available?
If you Google for "python user input", the first result (for me) points to a tutorial on how to get user input, and then what to do with that input depending on if it is a string or an integer, etc. You can see that here: http://anh.cs.luc.edu/python/hands-o...onHtml/io.html

Third, is the code you posted your own code, or sample code that was provided to you? In either event, add the "get date from user" lines of code to your "main" function, and try to convert the dates as requested. Do you know how to do this yet, or where to start looking to do this? If not, here's a hint: slice <- mouse-over to see

Show us what your new code looks like and let us know of any problems or errors you run into.

 2016-11-27, 05:16 #4 tanaydin   Oct 2016 816 Posts Hello, You should use input() or raw_input() function... something like that.. Code: def main(): start_date = raw_input("Start Date (e.g., 2013-11-01) ? ") end_date = raw_input("End Date (e.g., 2014-11-01) ? ") print(getAverageTemps(start_date, end_date))

 Similar Threads Thread Thread Starter Forum Replies Last Post jrafanelli Software 2 2018-01-11 15:16 flouran Programming 0 2009-07-25 02:43 starrynte Programming 1 2008-12-30 22:31 nuggetprime Programming 56 2008-10-27 22:45 R.D. Silverman Programming 18 2005-08-09 13:14

All times are UTC. The time now is 07:27.

Tue Nov 24 07:27:28 UTC 2020 up 75 days, 4:38, 4 users, load averages: 1.58, 1.82, 1.68