Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e97cdb6

Browse files
Merge pull request #977 from Rupesh-Singh-Karki/feat/uv_sunscreen_advisor
Added a new feature uv_sunscreen_advisor
2 parents 3021b08 + 40a6f44 commit e97cdb6

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

‎uv_sunscreen_advisor/README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# UV Index and Sunscreen Advisor
2+
3+
This Python script fetches the current UV index for a given location and provides personalized sunscreen advice based on the UV index and whether the user is indoors or outdoors.
4+
5+
## Features
6+
7+
- Fetches real-time UV index data based on user-provided latitude and longitude.
8+
- Provides personalized sunscreen advice based on UV exposure:
9+
- Low UV index: minimal protection recommended.
10+
- Moderate to extreme UV index: higher SPF and protective measures are suggested.
11+
- Differentiates advice for indoor and outdoor scenarios.
12+
13+
## Requirements
14+
15+
- Python 3.x
16+
- `requests` library for making HTTP API requests.
17+
18+
## Setup
19+
20+
### Install the required libraries:
21+
22+
```bash
23+
pip install requests
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /