|
| 1 | + |
| 2 | +# Weather Reporter (Simulated) |
| 3 | + |
| 4 | +A simple Python project that gives a mock weather report |
| 5 | +for a city entered by the user. |
| 6 | + |
| 7 | +This version doesn’t use any API — it randomly generates temperature, |
| 8 | +humidity, and conditions for demonstration and practice. |
| 9 | + |
| 10 | +Concepts covered: |
| 11 | +- Random number generation |
| 12 | +- Lists and string formatting |
| 13 | +- Functions and modular programming |
| 14 | + |
| 15 | +``` |
| 16 | +import random |
| 17 | +import time |
| 18 | +
|
| 19 | +
|
| 20 | +def get_mock_weather(city): |
| 21 | + """ |
| 22 | + Generates a simulated weather report for the given city. |
| 23 | +
|
| 24 | + Parameters: |
| 25 | + city (str): The name of the city |
| 26 | +
|
| 27 | + Returns: |
| 28 | + dict: A dictionary containing weather details |
| 29 | + """ |
| 30 | +
|
| 31 | + # Randomly generated values to simulate real weather |
| 32 | + temperature = random.randint(15, 40) # Temperature in °C |
| 33 | + humidity = random.randint(30, 90) # Humidity in % |
| 34 | + wind_speed = round(random.uniform(1.5, 10.0), 1) # Wind speed in km/h |
| 35 | +
|
| 36 | + # Possible weather conditions |
| 37 | + conditions = ["Sunny", "Cloudy", "Rainy", "Windy", "Stormy", "Foggy", "Clear"] |
| 38 | + condition = random.choice(conditions) |
| 39 | +
|
| 40 | + return { |
| 41 | + "city": city.title(), |
| 42 | + "temperature": temperature, |
| 43 | + "humidity": humidity, |
| 44 | + "wind_speed": wind_speed, |
| 45 | + "condition": condition, |
| 46 | + } |
| 47 | +
|
| 48 | +
|
| 49 | +def display_weather(report): |
| 50 | + """ |
| 51 | + Displays the weather report in a formatted way. |
| 52 | +
|
| 53 | + Parameters: |
| 54 | + report (dict): The dictionary containing weather info |
| 55 | + """ |
| 56 | + print("\n------------------------------------") |
| 57 | + print(f"Weather Report for: {report['city']}") |
| 58 | + print("------------------------------------") |
| 59 | + print(f"Temperature : {report['temperature']}°C") |
| 60 | + print(f"Humidity : {report['humidity']}%") |
| 61 | + print(f"Wind Speed : {report['wind_speed']} km/h") |
| 62 | + print(f"Condition : {report['condition']}") |
| 63 | + print("------------------------------------\n") |
| 64 | +
|
| 65 | +
|
| 66 | +def main(): |
| 67 | + Main function to handle user input and display weather info. |
| 68 | + print("Welcome to the Weather Reporter!") |
| 69 | + print("Type 'exit' anytime to quit.\n") |
| 70 | +
|
| 71 | + while True: |
| 72 | + city = input("Enter city name: ").strip() |
| 73 | +
|
| 74 | + if city.lower() == "exit": |
| 75 | + print("\nExiting Weather Reporter. Have a great day!") |
| 76 | + break |
| 77 | +
|
| 78 | + if not city: |
| 79 | + print("Please enter a valid city name.\n") |
| 80 | + continue |
| 81 | +
|
| 82 | + print("\nFetching weather report...") |
| 83 | + time.sleep(1.5) # Simulate data loading delay |
| 84 | +
|
| 85 | + report = get_mock_weather(city) |
| 86 | + display_weather(report) |
| 87 | +
|
| 88 | +
|
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
| 91 | +``` |
0 commit comments