Appearance
question:How can I create a Python program to track the stock prices of a specific company and send me an email notification when the price crosses a certain threshold?
answer:import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import requests import time # Function to get the current stock price def get_stock_price(api_url, symbol): response = requests.get(f"{api_url}?symbol={symbol}") data = response.json() current_price = data['price'] return current_price # Function to send email notification def send_email_notification(from_email, from_password, to_email, subject, body): msg = MIMEMultipart() msg['From'] = from_email msg['To'] = to_email msg['Subject'] = subject msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(from_email, from_password) text = msg.as_string() server.sendmail(from_email, to_email, text) server.quit() # Main function to track the stock price def track_stock_price(api_url, symbol, threshold, from_email, from_password, to_email): while True: current_price = get_stock_price(api_url, symbol) if current_price >= threshold: subject = f"Stock Price Alert: {symbol}" body = f"The stock price of {symbol} has reached {current_price}, which is above your threshold of {threshold}." send_email_notification(from_email, from_password, to_email, subject, body) break time.sleep(60) # Check the price every minute # Example usage if __name__ == "__main__": api_url = "https://api.example.com/stock" symbol = "AAPL" threshold = 150.00 from_email = "[email protected]" from_password = "your_password" to_email = "[email protected]" track_stock_price(api_url, symbol, threshold, from_email, from_password, to_email)
question:Given a list of numbers, write a Python function that returns a new list containing only the even numbers from the original list, but with a twist: the function should also sort these even numbers in descending order. Additionally, if the list contains any strings that can be converted to integers, the function should handle this gracefully and include these converted integers in the result if they are even. If a string cannot be converted to an integer, it should be ignored.
answer:def filter_and_sort_even_numbers(input_list): Returns a new list containing only the even numbers from the input_list, sorted in descending order. Any strings that can be converted to integers are also considered if they are even. even_numbers = [] for item in input_list: try: number = int(item) if number % 2 == 0: even_numbers.append(number) except (ValueError, TypeError): continue return sorted(even_numbers, reverse=True)
question:Can you help me write a Python function to find the median of a list of numbers? I need to be able to use this function in my project where I analyze data from surveys. The function should take a list of numbers as input and return the median. It would be great if you could also include a brief explanation of how the function works.
answer:def find_median(numbers): Returns the median of a list of numbers. The list can be of even or odd length. The function sorts the list, and then either returns the middle element (if odd) or the average of the two middle elements (if even). sorted_numbers = sorted(numbers) n = len(sorted_numbers) if n == 0: raise ValueError("The list is empty, median is undefined.") middle = n // 2 if n % 2 == 1: # odd length, return the middle element return sorted_numbers[middle] else: # even length, return the average of the two middle elements return (sorted_numbers[middle - 1] + sorted_numbers[middle]) / 2
question:How can I create a simple Python script to automate the process of renaming a batch of files in a directory, adding a prefix to their names?
answer:import os def rename_files_with_prefix(directory, prefix): Renames all files in the specified directory by adding the given prefix to their names. for filename in os.listdir(directory): if os.path.isfile(os.path.join(directory, filename)): new_name = prefix + filename os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))