mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   Python Coding Help? (https://www.mersenneforum.org/showthread.php?t=21729)

 kelzo 2016-11-09 01:51

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..

[I]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.[/I]

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)
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.

[I]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[/I]

 LaurV 2016-11-09 04:34

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.

 WraithX 2016-11-09 05:59

[QUOTE=kelzo;446790]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.

[I]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[/I][/QUOTE]

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()
[/CODE]

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: [url]http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html[/url]

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: [SPOILER]slice[/SPOILER] <- mouse-over to see

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

 tanaydin 2016-11-27 05:16

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))
[/CODE]

 All times are UTC. The time now is 19:54.