Question

Python Ask a user for their address. Based on their address, tell them what days they...

Python

Ask a user for their address. Based on their address, tell them what days they should bring an umbrella (>=50% chance of rain they should bring an umbrella).

Output example
Enter address: 15 Lisa Ct. Sewell NJ 08080

04/13/18 - Friday - Sun

04/14/18 - Saturday - Sun

04/15/18 - Sunday - Umbrella

04/16/18 - Monday - Sun

04/17/18 - Tuesday - Umbrella

04/18/18 - Wednesday - Sun

04/19/18 - Thursday - Umbrella

04/20/18 - Friday - Sun

Weather for another address? (y/n)

import requestsresponse = requests.get('https://www.google.com')
if response.status_code == 200:
    print('yay!')
else:
    print('oops!')

Use this website to obtain a lat/lon based on an address use:

https://my.locationiq.com/dashboard#geocoding

Use this website to obtain a forecast use:

https://darksky.net/dev

Example of request for location:

https://locationiq.org/v1/search.php?key=50ed712f145e62&q=15+Lisa+Ct.+Sewell%2C+NJ+08080&format=json

Starting point:

import requests
import pprintaddress = '15 Lisa Ct. Sewell NJ 08080'
response = requests.get('https://locationiq.org/v1/search.php?key=50ed712f145e62&q=' + address + '&format=json')
json = response.json()
lat = json[0]['lat']
lon = json[0]['lon']print(lat)
print(lon)
from datetime import datetime
print(datetime.fromtimestamp(1509993277))
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have provided screenshots of the code at the end because indentation in text is sometimes not retained in the answers, which can cause a lot of confusion especially for python.

Feel free to explore the API and use other different information from the response.
Finally, since a key for the darksky api wasn't provided I used a personal one for testing the code. I cannot share that key here for obvious reasons.
You can easily get a new key for yourself though and get 1000 requests per day for free.
This is required for the program to run

Code in Python:

import requests
import pprint
from datetime import datetime

repeat = True
while repeat:
repeat = False
address = input("\nEnter address: ")

#Get latitude and longitude of address by issuing a GET request to https://my.locationiq.com
#The key has already been provided in the question
response1 = requests.get('https://locationiq.org/v1/search.php?key=50ed712f145e62&q=' + address + '&format=json')

#Convert the response into a dictionary format so that we can work with it
json1 = response1.json()

#In this particular API response, the latitude and longitude are stored in 'lat' and 'lon' keys
lat = json1[0]['lat']
lon = json1[0]['lon']

#Now using these lat and lon values we will send a request to another API - the actual weather forecasting API
#Sign up for a darksky account and you will get a free key you can use for up to 1000 requests per day
#Use that key instead, it's really easy :)
#Simply plug in that key in here and the program is good to run
darksky_key = "GET ANOTHER KEY"

#Use the key to get a response
#This is the standard format for request as given in the Dark Sky docs:
#https://api.darksky.net/forecast/[key]/[latitude],[longitude]
response2 = requests.get("https://api.darksky.net/forecast/"+darksky_key+"/"+lat+","+lon);
json2 = response2.json()

#The response contains lots of information that we could use but here we only need the daily forecast
#By reading the docs, we find that we can access daily forecast data by:
#json2['daily']['data'][n]['precipProbability']
#where n is an integer representing the day: 0 is today, 1 is tomorrow and so on

#The timestamps for each day are also provided in the response so it makes sense to read them too
#This will help later to print the dates of the week
#List to store rain chances of the week
rain_chances = []
#List to store the timestamps of the days of the week
time_stamps = []

#Reading 8 values in total - current day and next 7 days
for i in range(0, 8):
rain_chances.append(json2['daily']['data'][i]['precipProbability'])
time_stamps.append(datetime.fromtimestamp(json2['daily']['data'][i]['time']))


#We safely have the rain chances for the week!
#We display the datetime objects in the specific mm/dd/yy format, the weekday is found by strftime("%A")
for i in range(0, 8):
#Since the probablities are measured as a number between 0 and 1, being greater than 0.5 means >= 50% chance
if rain_chances[i] >= 0.5:
message = "Umbrella"
else:
message = "Sun"
print (time_stamps[i].strftime("%m/%d/%y") + " - " + time_stamps[i].strftime("%A") + " - " + message)

#Ask for repeat
inp = input ("\nWeather for another address? (y/n): ")
inp = inp.lower()
if inp == 'y':
repeat = True

Sample Output:

Code Screenshot:

Add a comment
Know the answer?
Add Answer to:
Python Ask a user for their address. Based on their address, tell them what days they...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT