|
| 1 | +import requests |
| 2 | + |
| 3 | + |
| 4 | +# Function to get UV index from the API |
| 5 | +def get_uv_index(lat, lon, api_key): |
| 6 | + url = ( |
| 7 | + f"http://api.openweathermap.org/data/2.5/uvi?" |
| 8 | + f"lat={lat}&lon={lon}&appid={api_key}" |
| 9 | + ) |
| 10 | + response = requests.get(url) |
| 11 | + if response.status_code == 200: |
| 12 | + uv_data = response.json() |
| 13 | + return uv_data['value'] |
| 14 | + else: |
| 15 | + return None |
| 16 | + |
| 17 | + |
| 18 | +# Function to provide sunscreen advice based on UV index and location |
| 19 | +def sunscreen_advice(uv_index, indoor): |
| 20 | + if indoor: |
| 21 | + return ( |
| 22 | + "You are indoors, sunscreen is not necessary unless you're " |
| 23 | + "near windows with intense sunlight." |
| 24 | + ) |
| 25 | + |
| 26 | + advice = "" |
| 27 | + if uv_index < 3: |
| 28 | + advice = ( |
| 29 | + "Low UV index. No sunscreen is needed, but wearing sunglasses " |
| 30 | + "is a good idea." |
| 31 | + ) |
| 32 | + elif 3 <= uv_index < 6: |
| 33 | + advice = ( |
| 34 | + "Moderate UV index. Wear SPF 15-30 sunscreen and consider " |
| 35 | + "sunglasses and a hat." |
| 36 | + ) |
| 37 | + elif 6 <= uv_index < 8: |
| 38 | + advice = ( |
| 39 | + "High UV index. Use SPF 30-50 sunscreen, sunglasses, and " |
| 40 | + "seek shade if possible." |
| 41 | + ) |
| 42 | + elif 8 <= uv_index < 11: |
| 43 | + advice = ( |
| 44 | + "Very high UV index. Use SPF 50+ sunscreen, wear protective " |
| 45 | + "clothing, sunglasses, and avoid direct sun exposure between " |
| 46 | + "10 AM and 4 PM." |
| 47 | + ) |
| 48 | + else: |
| 49 | + advice = ( |
| 50 | + "Extreme UV index. Stay indoors or use very high SPF sunscreen, " |
| 51 | + "protective clothing, sunglasses, and avoid the sun as much " |
| 52 | + "as possible." |
| 53 | + ) |
| 54 | + |
| 55 | + return advice |
| 56 | + |
| 57 | + |
| 58 | +# Main function |
| 59 | +def main(): |
| 60 | + # Enter your details |
| 61 | + lat = input("Enter your latitude: ") |
| 62 | + lon = input("Enter your longitude: ") |
| 63 | + indoor = input("Are you indoors? (yes/no): ").lower() == 'yes' |
| 64 | + |
| 65 | + # Enter your API key here |
| 66 | + # Visit this link to get your API key - https://openweathermap.org/api |
| 67 | + api_key = 'your_api_key_here' |
| 68 | + |
| 69 | + # Fetch the UV-Index at the input latitude and longitude |
| 70 | + uv_index = get_uv_index(lat, lon, api_key) |
| 71 | + |
| 72 | + if uv_index is not None: |
| 73 | + print(f"Current UV index: {uv_index}") |
| 74 | + # Provide sunscreen advice |
| 75 | + advice = sunscreen_advice(uv_index, indoor) |
| 76 | + print(advice) |
| 77 | + else: |
| 78 | + print("Error fetching UV index data.") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments