diff --git a/calculator.py b/calculator.py new file mode 100644 index 00000000..69f7acf0 --- /dev/null +++ b/calculator.py @@ -0,0 +1,45 @@ + + +def sumar(a, b): + return a + b + +def restar(a, b): + return a - b + +def multiplicar(a, b): + return a * b + +def dividir(a, b): + if b != 0: + return a / b + else: + return "Error: División por cero" + +def calculadora(): + print("Seleccione una operación:") + print("1. Sumar") + print("2. Restar") + print("3. Multiplicar") + print("4. Dividir") + + opcion = input("Ingrese el número de la operación: ") + + try: + num1 = float(input("Ingrese el primer número: ")) + num2 = float(input("Ingrese el segundo número: ")) + + if opcion == '1': + print("Resultado:", sumar(num1, num2)) + elif opcion == '2': + print("Resultado:", restar(num1, num2)) + elif opcion == '3': + print("Resultado:", multiplicar(num1, num2)) + elif opcion == '4': + print("Resultado:", dividir(num1, num2)) + else: + print("Opción no válida") + except ValueError: + print("Error: Entrada no válida") + +if __name__ == "__main__": + calculadora() \ No newline at end of file diff --git a/card_draw.py b/card_draw.py index 4f4bf631..bc4bd7af 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'])) # Fixed missing parenthesis # 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): # Fixed missing colon + print(deck[i][0], "of", deck[i][1]) # Fixed missing parenthesis diff --git a/hello.py b/hello.py new file mode 100644 index 00000000..cd69d0dd --- /dev/null +++ b/hello.py @@ -0,0 +1 @@ +print("Hola Mundo") diff --git a/sum_elements.py b/sum_elements.py index 2731a254..d8d5de3c 100644 --- a/sum_elements.py +++ b/sum_elements.py @@ -1,37 +1,39 @@ -#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 - MAX = 100 def calculate_sum(arr): - result = 0 - for num in arr: - result += num - return result + return sum(arr) # Usar la función integrada sum() + +def get_valid_input(prompt, validation_fn): + while True: + try: + value = int(input(prompt)) + if validation_fn(value): + return value + print("Invalid input. Please try again.") + except ValueError: + print("Invalid input. Please enter a valid integer.") + +def get_number_of_elements(): + return get_valid_input( + "Enter the number of elements (1-100): ", + lambda x: 1 <= x <= MAX + ) + +def get_elements(n): + arr = [] + print(f"Enter {n} integers:") + for _ in range(n): + arr.append(get_valid_input("Enter an integer: ", lambda x: True)) + return arr def main(): - try: - n = int(input("Enter the number of elements (1-100): ")) - if not 1 <= n <= MAX: - print("Invalid input. Please provide a digit ranging from 1 to 100.") - exit(1) - - arr = [] - - print(f"Enter {n} integers:") - for _ in range(n): - try: - arr.append(int(input())) - except ValueError: - print("Invalid input. Please enter valid integers.") - exit(1) - - total = calculate_sum(arr) - - print("Sum of the numbers:", total) - - except KeyboardInterrupt: - print("\nProgram terminated by user.") - exit(1) + try: + n = get_number_of_elements() + arr = get_elements(n) + print("Sum of the numbers:", calculate_sum(arr)) # Llamar directamente a calculate_sum() + except KeyboardInterrupt: + print("\nProgram terminated by user.") + exit(1) if __name__ == "__main__": - main() + main() diff --git a/weather_script.py b/weather_script.py new file mode 100644 index 00000000..1f90addb --- /dev/null +++ b/weather_script.py @@ -0,0 +1,30 @@ +import requests + +class WeatherAPI: + def __init__(self, api_key): + self.api_key = api_key + self.base_url = "https://api.openweathermap.org/data/2.5/weather" + + def get_weather(self, city_name, units="metric"): + params = { + "q": city_name, + "appid": self.api_key, + "units": units + } + try: + response = requests.get(self.base_url, params=params) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + print(f"Error fetching weather data: {e}") + return None + +# Example usage: +# Replace 'your_api_key_here' with your OpenWeatherMap API key +if __name__ == "__main__": + api_key = "your_api_key_here" + weather_api = WeatherAPI(api_key) + city = "London" + weather_data = weather_api.get_weather(city) + if weather_data: + print(weather_data) \ No newline at end of file