Thuta Learning
ရှာဖွေရန်
ProjectsProgrammingbeginner

Weather App (Enhanced)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

🐍 Lesson 43: Weather App (Python Project)

1. Project Overview

မြန်မာ → Weather App ဆိုတာ Python project တစ်ခုအနေနဲ့ API integration ကို လေ့ကျင့်ဖို့ အဆင်ပြေတဲ့ project ဖြစ်တယ်။ User က မြို့နာမည်ထည့်လိုက်တာနဲ့ အဲဒီမြို့ရဲ့ အပူချိန်၊ အခြေအနေ စတာတွေကို ပြပေးမယ်။

English → A Weather App is a great beginner project to practice API integration. The user enters a city name, and the app fetches weather data like temperature and conditions.

2. Why Build a Weather App?

  • API (Application Programming Interface) ကို ချိတ်သုံးနည်း လေ့ကျင့်နိုင်မယ်
  • JSON data ကို parse လုပ်နည်း သင်ယူနိုင်မယ်
  • CLI (Command Line Interface) version နဲ့ GUI version နှစ်မျိုးလုံး စမ်းနိုင်မယ်

3. အကျဉ်းချုပ်

✅ Weather App = API integration practice project

✅ Use OpenWeatherMap API or similar

✅ Parse JSON response data

✅ CLI and GUI versions possible

python
# ===== 1. Weather App Setup =====
import requests

API_KEY = "YOUR_API_KEY"  # Replace with actual API key
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"

# ===== 2. CLI Version =====
print("===== CLI Weather App =====")

def get_weather(city):
    url = f"{BASE_URL}?q={city}&appid={API_KEY}&units=metric"
    
    try:
        response = requests.get(url)
        data = response.json()
        
        if data["cod"] == 200:
            temp = data["main"]["temp"]
            desc = data["weather"][0]["description"]
            humidity = data["main"]["humidity"]
            
            return f"Temperature: {temp}°C\nCondition: {desc}\nHumidity: {humidity}%"
        else:
            return "City not found!"
    except Exception as e:
        return f"Error: {e}"

# Example usage (commented - requires API key)
# city = input("Enter city: ")
# print(get_weather(city))

# ===== 3. Error Handling =====
print(f"\n===== Error Handling =====")
print("✅ Check API response status code")
print("✅ Handle network errors (try...except)")
print("✅ Validate city name exists")

# ===== 4. API Response Structure =====
print(f"\n===== JSON Response Structure =====")
print("Response contains:")
print("- main.temp → temperature")
print("- weather[0].description → condition")
print("- main.humidity → humidity")
print("- cod → status code (200 = success)")

# ===== 5. Notes =====
print(f"\n===== Setup Notes =====")
print("1. Get free API key from openweathermap.org")
print("2. Install: pip install requests")
print("3. Replace 'YOUR_API_KEY' with actual key")
print("4. API has rate limits (free tier)")
You should see
===== CLI Weather App ===== ===== Error Handling ===== ✅ Check API response status code ✅ Handle network errors (try...except) ✅ Validate city name exists ===== JSON Response Structure ===== Response contains: - main.temp → temperature - weather[0].description → condition - main.humidity → humidity - cod → status code (200 = success) ===== Setup Notes ===== 1. Get free API key from openweathermap.org 2. Install: pip install requests 3. Replace 'YOUR_API_KEY' with actual key 4. API has rate limits (free tier)
Weather App (Enhanced) | Thuta Learning