From bcd2b9c02ed9c403263371ed0ba922a64ced24e3 Mon Sep 17 00:00:00 2001 From: Oszkar Perecz Date: Thu, 27 Mar 2025 19:48:39 +0000 Subject: [PATCH 1/5] Program Hello World - initial commit --- hello.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 hello.py diff --git a/hello.py b/hello.py new file mode 100644 index 00000000..73c6bec2 --- /dev/null +++ b/hello.py @@ -0,0 +1,5 @@ +def main(): + print("Hello, World!") + +if __name__ == "__main__": + main() From af94c3d18eebfa5a7f4226b4b5438a06334f8f2b Mon Sep 17 00:00:00 2001 From: Oszkar Perecz Date: Thu, 27 Mar 2025 20:08:35 +0000 Subject: [PATCH 2/5] Add hello.py with a simple Hello World program --- calculator.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ calculator.py | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 calculator.js create mode 100644 calculator.py diff --git a/calculator.js b/calculator.js new file mode 100644 index 00000000..1d85775c --- /dev/null +++ b/calculator.js @@ -0,0 +1,56 @@ +function add(a, b) { + return a + b; +} + +function subtract(a, b) { + return a - b; +} + +function multiply(a, b) { + return a * b; +} + +function divide(a, b) { + if (b === 0) { + return "Error: Division by zero is not allowed."; + } + return a / b; +} + +function calculator() { + console.log("Basic Calculator"); + console.log("Select operation:"); + console.log("1. Add"); + console.log("2. Subtract"); + console.log("3. Multiply"); + console.log("4. Divide"); + + const choice = prompt("Enter choice (1/2/3/4):"); + + if (["1", "2", "3", "4"].includes(choice)) { + const num1 = parseFloat(prompt("Enter first number:")); + const num2 = parseFloat(prompt("Enter second number:")); + + let result; + switch (choice) { + case "1": + result = add(num1, num2); + break; + case "2": + result = subtract(num1, num2); + break; + case "3": + result = multiply(num1, num2); + break; + case "4": + result = divide(num1, num2); + break; + } + console.log(`The result is: ${result}`); + } else { + console.log("Invalid input. Please select a valid operation."); + } +} + +// Run the calculator +calculator(); \ No newline at end of file diff --git a/calculator.py b/calculator.py new file mode 100644 index 00000000..2dd0bea6 --- /dev/null +++ b/calculator.py @@ -0,0 +1,41 @@ +def add(a, b): + return a + b + +def subtract(a, b): + return a - b + +def multiply(a, b): + return a * b + +def divide(a, b): + if b == 0: + return "Error: Division by zero is not allowed." + return a / b + +def calculator(): + print("Basic Calculator") + 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"The result is: {add(num1, num2)}") + elif choice == '2': + print(f"The result is: {subtract(num1, num2)}") + elif choice == '3': + print(f"The result is: {multiply(num1, num2)}") + elif choice == '4': + print(f"The result is: {divide(num1, num2)}") + else: + print("Invalid input. Please select a valid operation.") + +if __name__ == "__main__": + calculator() \ No newline at end of file From 1205d630269d1f36e277e9543e62af28e7215c50 Mon Sep 17 00:00:00 2001 From: Oszkar Perecz Date: Thu, 27 Mar 2025 20:20:09 +0000 Subject: [PATCH 3/5] Added 2 files for weather_script, in python and js --- weather_script.js | 33 +++++++++++++++++++++++++++++++++ weather_script.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 weather_script.js create mode 100644 weather_script.py diff --git a/weather_script.js b/weather_script.js new file mode 100644 index 00000000..b6a56670 --- /dev/null +++ b/weather_script.js @@ -0,0 +1,33 @@ +const fetch = require('node-fetch'); // Ensure you have node-fetch installed (npm install node-fetch) + +async function fetchWeather(cityName, apiKey) { + const baseUrl = "http://api.openweathermap.org/data/2.5/weather"; + const url = `${baseUrl}?q=${cityName}&appid=${apiKey}&units=metric`; // Use "imperial" for Fahrenheit + + try { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + const weatherData = await response.json(); + return weatherData; + } catch (error) { + return { error: error.message }; + } +} + +(async () => { + const API_KEY = "your_api_key_here"; // Replace with your OpenWeatherMap API key + const city = "London"; // Replace with the desired city name + + const weatherData = await fetchWeather(city, API_KEY); + + if (weatherData.error) { + console.log(`Error: ${weatherData.error}`); + } else { + console.log(`Weather in ${weatherData.name}:`); + console.log(`Temperature: ${weatherData.main.temp}°C`); + console.log(`Weather: ${weatherData.weather[0].description.charAt(0).toUpperCase() + weatherData.weather[0].description.slice(1)}`); + console.log(`Humidity: ${weatherData.main.humidity}%`); + } +})(); \ No newline at end of file diff --git a/weather_script.py b/weather_script.py new file mode 100644 index 00000000..8392c7b3 --- /dev/null +++ b/weather_script.py @@ -0,0 +1,41 @@ +import requests + +def fetch_weather(city_name, api_key): + """ + Fetch weather data for a given city from OpenWeatherMap API. + + Args: + city_name (str): Name of the city to fetch weather for. + api_key (str): Your OpenWeatherMap API key. + + Returns: + dict: Weather data if successful, or an error message. + """ + base_url = "http://api.openweathermap.org/data/2.5/weather" + params = { + "q": city_name, + "appid": api_key, + "units": "metric" # Use "imperial" for Fahrenheit + } + + try: + response = requests.get(base_url, params=params) + response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx) + return response.json() + except requests.exceptions.RequestException as e: + return {"error": str(e)} + +if __name__ == "__main__": + # Replace with your OpenWeatherMap API key + API_KEY = "your_api_key_here" + city = input("Enter the city name: ") + + weather_data = fetch_weather(city, API_KEY) + + if "error" in weather_data: + print(f"Error: {weather_data['error']}") + else: + print(f"Weather in {weather_data['name']}:") + print(f"Temperature: {weather_data['main']['temp']}°C") + print(f"Weather: {weather_data['weather'][0]['description'].capitalize()}") + print(f"Humidity: {weather_data['main']['humidity']}%") \ No newline at end of file From 1f1332e7be3f9511fbf4bd621d2a47d51edeb80c Mon Sep 17 00:00:00 2001 From: Oszkar Perecz Date: Thu, 27 Mar 2025 20:31:10 +0000 Subject: [PATCH 4/5] refactored sum_elements.py to make it more readable and maintainable --- sum_elements.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/sum_elements.py b/sum_elements.py index 2731a254..544f8f4a 100644 --- a/sum_elements.py +++ b/sum_elements.py @@ -1,5 +1,26 @@ #A poorly written example of a program in Python. It prompts the user for the number of elements to sum, takes those integers as input, and handles some basic error cases - +def get_number_of_elements(max_value): + while True: + try: + n = int(input(f"Enter the number of elements (1-{max_value}): ")) + if 1 <= n <= max_value: + return n + else: + print(f"Invalid input. Please provide a number between 1 and {max_value}.") + except ValueError: + print("Invalid input. Please enter a valid integer.") + +def get_elements(n): + elements = [] + print(f"Enter {n} integers:") + for _ in range(n): + while True: + try: + elements.append(int(input())) + break + except ValueError: + print("Invalid input. Please enter a valid integer.") + return elements MAX = 100 def calculate_sum(arr): From ade3a71f3f8800eb4f2d995569887b1d95e52475 Mon Sep 17 00:00:00 2001 From: Oszkar Perecz Date: Thu, 27 Mar 2025 20:33:28 +0000 Subject: [PATCH 5/5] fixed card_draw.py syntax errors --- card_draw.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/card_draw.py b/card_draw.py index 4f4bf631..b8e2ae4e 100644 --- a/card_draw.py +++ b/card_draw.py @@ -4,12 +4,12 @@ import itertools, random # make a deck of cards -deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']) +deck = list(itertools.product(range(1, 14), ['Spade', 'Heart', 'Diamond', 'Club'])) # shuffle the cards random.shuffle(deck) # draw five cards print("You got:") -for i in range(5) - print(deck[i][0], "of", deck[i][1] +for i in range(5): + print(deck[i][0], "of", deck[i][1])