Weather Forecast Using Python – Simple Implementation

Filed Under: Python
Weather Forecast Python

The weather has a great impact on how we go on about our day-to-day activities. In this tutorial, we will use Python to help us to display weather forecast reports of a particular city in a very attractive manner.

let’s first understand what weather forecasting means. It is a process in which humans/machines try to predict the conditions of the weather for a given location. Now you might be wondering, how is weather forecasting going to benefit society? And why do I even need to build it?

Well, weather prediction can positively impact people’s lives in various ways, I will list a few of them below

  1. Tourism is strongly affected by weather of a certain place
  2. It strongly impacts the safety and operation of transportation of all forms.
  3. We even decide our outfits based on the weather conditions
  4. No doubt, it plays a major role in the farming business.

Now that we are clear with the importance of the application we aim to develop, let’s begin the code implementation.

Code Implementation

We will be needing the requests library of python, which we need to install in the system using the pip command in the CMD of your computer.

pip install requests

Now, we need to make sure that we introduce the application to the users properly. Look at the code snippet below. You can change the texts according to your preferences.

print("\t\tWelcome to the Weather Forecaster!\n\n")
print("Just Enter the City you want the weather report for and click on the button! It's that simple!\n\n")

Next, let’s take the input of the city name from the user using the code snippet below.

city_name = input("Enter the name of the City : ")

Now, we have the basic stuff ready and we are all set to move to the major part of the project. We will start off by importing the requests module that we just installed in our system earlier.

import requests

We will create a function that will take the name of the city the user enters and prints the report for us. Look at the code of the function below.

def Gen_report(C):
    url = 'https://wttr.in/{}'.format(C)
    try:
        data = requests.get(url)
        T = data.text
    except:
        T = "Error Occurred"
    print(T)
Gen_report(city_name)

To generate the weather report we will make use of wttr. Now, you might be wondering what wttr is? wttr is a console-oriented weather forecast service that comes with a number of information representation ways to make sure you get the weather data in the best form possible.

Finally, we just need to request the data from the wttr link generated with the help of the requests module. We are making use of the try-except block of Python to make sure that we handle possible errors beforehand.

Recommended Read: Python Exception Handling – Python try except

The Complete Code for Implementing Weather Forecasts in Python

Let’s have a look at the complete code that we just coded in the previous section.

import requests

print("\t\tWelcome to the Weather Forecaster!\n\n")
print("Just Enter the City you want the weather report for and click on the button! It's that simple!\n\n")

city_name = input("Enter the name of the City : ")
print("\n\n")

# Function to Generate Report
def Gen_report(C):
    url = 'https://wttr.in/{}'.format(C)
    try:
        data = requests.get(url)
        T = data.text
    except:
        T = "Error Occurred"
    print(T)
    
Gen_report(city_name)

Output Screenshots

WeatherForecastPython
WeatherForecastPython

Conclusion

That’s how easy it is! You now have your personal terminal-based weather station using Python. Experiment with the output to see what you can do with it. Maybe even create a weather-forecasting website for yourself! Let us know what you did with the outputs you receive here.

close
Generic selectors
Exact matches only
Search in title
Search in content