![]() |
![]() |
#1 |
Nov 2016
116 Posts |
![]()
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 |
![]() |
![]() |
![]() |
#2 |
Romulan Interpreter
"name field"
Jun 2011
Thailand
3×23×149 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.
|
![]() |
![]() |
![]() |
#3 | |
Mar 2006
10268 Posts |
![]() Quote:
An example will look like this: Code:
def main(): print(getAverageTemps("2013-11-01", "2014-10-31")) if __name__ == "__main__": main() 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. |
|
![]() |
![]() |
![]() |
#4 |
Oct 2016
32 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)) |
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Anyone know enough about coding to do this? | jrafanelli | Software | 2 | 2018-01-11 15:16 |
Zhang's OPQBT coding help? | flouran | Programming | 0 | 2009-07-25 02:43 |
coding midlet for TF | starrynte | Programming | 1 | 2008-12-30 22:31 |
Questions on coding my FFT&LLR implementation | nuggetprime | Programming | 56 | 2008-10-27 22:45 |
Coding Challenges | R.D. Silverman | Programming | 18 | 2005-08-09 13:14 |