Share your repls and programming experiences

← Back to all posts
Weather forecasting app
IndyRishi (151)

An app that can forecast weather by house address.

Comments
hotnewtop
RahulChoubey1 (173)

I have an idea. For convenience, use the str.replace() method so the user doesn't have to type pluses. Also, add spaces in front of the input requests to make them look better.

And don't forget f-strings!

Finished code:

import requests

street = input("Street: ")
city = input("City: ")
state = input("Use abbreviation. State: ")
zipcode = input("Zipcode: ")
street = street.replace(" ", "+")
city = city.replace(" ", "+")

geocode = requests.get(f"https://geocoding.geo.census.gov/geocoder/locations/address?street={street}&city={city}&state={state}&zip={zipcode}&benchmark=4&format=json")

#print(geocode.json()["result"]["addressMatches"])
coordinates = geocode.json()["result"]["addressMatches"][0]["coordinates"]

gridpoints = requests.get("https://api.weather.gov/points/{},{}".format(coordinates["y"],coordinates["x"]))

forecast = requests.get(gridpoints.json()["properties"]["forecast"])
for days in forecast.json()["properties"]["periods"]:
    print("{}:\n{}{}, {}".format(days["name"],days["temperature"],days["temperatureUnit"],days["shortForecast"]))


#for keys in gridpoints.json()["properties"]["forecast"]:
 #   print(keys)
#print(geocode)
#for keys in geocode.json()["result"]["addressMatches"][0]["coordinates"]["x"]:
 #   print(keys)
#print(geocode.json())
IndyRishi (151)

@RahulChoubey1 I tried the code you sent. It didn't work.

damienbois82 (0)

@IndyRishi Impressive how you got this in only 26 lines!

IndyRishi (151)

@damienbois82 Thank you! By the way, if you type "Thank you" without an exclamation mark, it will look weird.

peternielsen112 (38)

It doesn't seem to work... any ideas?
Traceback (most recent call last):
File "main.py", line 11, in <module>
coordinates = geocode.json()["result"][0]
IndexError: list index out of range

IndyRishi (151)

@peternielsen112 It has to be a house. Not a school or apartment. Sorry for any inconvienience!

IndyRishi (151)

@IndyRishi This is weird. I tried it at my school and I got the same error

IndyRishi (151)

@peternielsen112 Please send a screen shot of the error

IndyRishi (151)

Did you capitalize every first letter and use+pluses+as+spaces? Also make sure that the state is abbreviated and capitalized.

220723j (4)

@peternielsen112 same thing happened to me

IndyRishi (151)

@peternielsen112 Oooh. You forgot the house number.

IndyRishi (151)

@220723j Please send me the error message and make sure all the inputs are typed correctly.

IndyRishi (151)

@peternielsen112 Like for example, 123+Example+Drive. You forgot the 123 part.

peternielsen112 (38)

@IndyRishi this showed

Use pluses as spaces and include the house number. Street:470795+Emerson+Court
Use pluses as spaces. City:Susanville
Use abbreviation. State:CA
Zipcode:96130
Traceback (most recent call last):
File "main.py", line 11, in <module>
coordinates = geocode.json()["result"][0]
IndexError: list index out of range