From fb2293b50c6368f964141fcb299d95006cf05b5b Mon Sep 17 00:00:00 2001 From: github_cloudlabsuser_1243 Date: Tue, 18 Feb 2025 17:02:07 +0000 Subject: [PATCH] weather --- calculator.py | 43 +++++++++++++++++++++++++++++++++++++++++++ hello.py | 1 + weather_script.py | 27 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 calculator.py create mode 100644 hello.py create mode 100644 weather_script.py diff --git a/calculator.py b/calculator.py new file mode 100644 index 00000000..5049d6c1 --- /dev/null +++ b/calculator.py @@ -0,0 +1,43 @@ +def add(x, y): + return x + y + +def subtract(x, y): + return x - y + +def multiply(x, y): + return x * y + +def divide(x, y): + if y == 0: + raise ValueError("Cannot divide by zero!") + return x / y + +def main(): + print("Select operation:") + print("1. Add") + print("2. Subtract") + print("3. Multiply") + print("4. Divide") + + choice = input("Enter choice(1/2/3/4): ") + + if choice in ['1', '2', '3', '4']: + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + + if choice == '1': + print(f"{num1} + {num2} = {add(num1, num2)}") + elif choice == '2': + print(f"{num1} - {num2} = {subtract(num1, num2)}") + elif choice == '3': + print(f"{num1} * {num2} = {multiply(num1, num2)}") + elif choice == '4': + try: + print(f"{num1} / {num2} = {divide(num1, num2)}") + except ValueError as e: + print(e) + else: + print("Invalid input") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/hello.py b/hello.py new file mode 100644 index 00000000..4648e701 --- /dev/null +++ b/hello.py @@ -0,0 +1 @@ +print("Hello, World!") \ No newline at end of file diff --git a/weather_script.py b/weather_script.py new file mode 100644 index 00000000..e8d88700 --- /dev/null +++ b/weather_script.py @@ -0,0 +1,27 @@ +import requests + +def get_weather(api_key, city): + base_url = "http://api.openweathermap.org/data/2.5/weather" + params = { + 'q': city, + 'appid': api_key, + 'units': 'metric' + } + response = requests.get(base_url, params=params) + if response.status_code == 200: + data = response.json() + main = data['main'] + weather = data['weather'][0] + temperature = main['temp'] + humidity = main['humidity'] + weather_description = weather['description'] + print(f"Temperature: {temperature}°C") + print(f"Humidity: {humidity}%") + print(f"Weather Condition: {weather_description}") + else: + print("Error in the HTTP request") + +if __name__ == "__main__": + api_key = "your_api_key_here" + city = input("Enter city name: ") + get_weather(api_key, city) \ No newline at end of file