mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2016-11-09, 01:51   #1
kelzo
 
Nov 2016

1 Posts
Default 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
kelzo is offline   Reply With Quote
Old 2016-11-09, 04:34   #2
LaurV
Romulan Interpreter
 
LaurV's Avatar
 
Jun 2011
Thailand

2×3×31×47 Posts
Default

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.
LaurV is offline   Reply With Quote
Old 2016-11-09, 05:59   #3
WraithX
 
WraithX's Avatar
 
Mar 2006

23·59 Posts
Default

Quote:
Originally Posted by kelzo View Post
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.
WraithX is offline   Reply With Quote
Old 2016-11-27, 05:16   #4
tanaydin
 
Oct 2016

10002 Posts
Default

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))
tanaydin is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
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

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

Fri Sep 25 07:53:17 UTC 2020 up 15 days, 5:04, 0 users, load averages: 2.07, 1.66, 1.46

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.