From 08a9a9d849ee5ef12ac74db0a7c0a9a069d61d3f Mon Sep 17 00:00:00 2001 From: Vachaspati Mishra Date: Wed, 25 Feb 2026 11:16:22 +0530 Subject: [PATCH 1/2] Added error handling --- tictactoe/tictactoe.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tictactoe/tictactoe.py b/tictactoe/tictactoe.py index ac79dfb..5212e43 100644 --- a/tictactoe/tictactoe.py +++ b/tictactoe/tictactoe.py @@ -107,12 +107,16 @@ def max_value(board): def min_value(board): """Returns the minimum value (for O) of a move.""" - if terminal(board): - return utility(board) - value = float('inf') - for action in actions(board): - value = min(value, max_value(result(board, action))) - return value + try: + if terminal(board): + return utility(board) + value = float('inf') + for action in actions(board): + value = min(value, max_value(result(board, action))) + return value + except Exception as e: + print(f"Exception: {e}") + return None if __name__ == "__main__": From de4569b2c89b896eec27f427eb9de460a62b7b0b Mon Sep 17 00:00:00 2001 From: Vachaspati Mishra Date: Thu, 26 Feb 2026 10:13:20 +0530 Subject: [PATCH 2/2] Added error handling --- Neuronal-Red/temperature_prediction.py | 113 +++++++++++++------------ Password_Manager/project.py | 77 +++++++++++------ 2 files changed, 107 insertions(+), 83 deletions(-) diff --git a/Neuronal-Red/temperature_prediction.py b/Neuronal-Red/temperature_prediction.py index b69b077..119d4cc 100644 --- a/Neuronal-Red/temperature_prediction.py +++ b/Neuronal-Red/temperature_prediction.py @@ -3,58 +3,61 @@ import numpy as np -# Input data -celsius = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=np.float32) -fahrenheit = (celsius*1.8) + 32 - - -# Model definition -Input_layer = tf.keras.layers.Input((1,)) -hidden1 = tf.keras.layers.Dense(units=2) #Dense Layer With 2 units so as to not overfit the data -output = tf.keras.layers.Dense(units=1) -model = tf.keras.Sequential([Input_layer,hidden1, output]) -# Model compilation -model.compile( - optimizer=tf.keras.optimizers.Adam(0.1), - loss='mean_squared_error' -) - -# Model training -epochs = 100 -print("Starting training...") -history = model.fit(celsius, fahrenheit, epochs=epochs, verbose=False) -loss = history.history["loss"] -print("Model trained!") - -# Making a prediction -print("Let's make a prediction!") -result = model.predict(np.array([89.0])) -print("The result is " + str(result) + " fahrenheit") -print(f"True value = {(89.0*1.8) +32}") - -# Viewing the internal variables of the model -print("Internal model variables") -print(hidden1.get_weights()) -# print(hidden2.get_weights()) -print(output.get_weights()) - -# loss changes -plt.figure(figsize = (10,10)) -plt.subplot(1,2,1) -plt.plot(range(epochs),loss) -plt.xlabel("Epochs") -plt.ylabel("loss") -plt.title("losse over epochs") - -# true values vs predicted values -plt.subplot(1,2,2) -x_test = np.random.randint(0,100,[20,1]) -y_test = (x_test*1.8) +32 -y_pred = model.predict(x_test) - -plt.scatter(x_test, y_test, color='blue', label='True Values') -plt.scatter(x_test, y_pred, color='red', label='Predicted Values') -plt.xlabel("True values") -plt.ylabel("predicted values") -plt.title("True values vs Predicted values") -plt.show() \ No newline at end of file +try: + # Input data + celsius = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=np.float32) + fahrenheit = (celsius*1.8) + 32 + + + # Model definition + Input_layer = tf.keras.layers.Input((1,)) + hidden1 = tf.keras.layers.Dense(units=2) #Dense Layer With 2 units so as to not overfit the data + output = tf.keras.layers.Dense(units=1) + model = tf.keras.Sequential([Input_layer,hidden1, output]) + # Model compilation + model.compile( + optimizer=tf.keras.optimizers.Adam(0.1), + loss='mean_squared_error' + ) + + # Model training + epochs = 100 + print("Starting training...") + history = model.fit(celsius, fahrenheit, epochs=epochs, verbose=False) + loss = history.history["loss"] + print("Model trained!") + + # Making a prediction + print("Let's make a prediction!") + result = model.predict(np.array([89.0])) + print("The result is " + str(result) + " fahrenheit") + print(f"True value = {(89.0*1.8) +32}") + + # Viewing the internal variables of the model + print("Internal model variables") + print(hidden1.get_weights()) + # print(hidden2.get_weights()) + print(output.get_weights()) + + # loss changes + plt.figure(figsize = (10,10)) + plt.subplot(1,2,1) + plt.plot(range(epochs),loss) + plt.xlabel("Epochs") + plt.ylabel("loss") + plt.title("losse over epochs") + + # true values vs predicted values + plt.subplot(1,2,2) + x_test = np.random.randint(0,100,[20,1]) + y_test = (x_test*1.8) +32 + y_pred = model.predict(x_test) + + plt.scatter(x_test, y_test, color='blue', label='True Values') + plt.scatter(x_test, y_pred, color='red', label='Predicted Values') + plt.xlabel("True values") + plt.ylabel("predicted values") + plt.title("True values vs Predicted values") + plt.show() +except Exception as e: + printf("Exception occured!", e) \ No newline at end of file diff --git a/Password_Manager/project.py b/Password_Manager/project.py index 3c695b1..f0a4abd 100644 --- a/Password_Manager/project.py +++ b/Password_Manager/project.py @@ -11,48 +11,69 @@ def main_menu(): """Display the main menu and get the user's choice.""" - menu = [ - "---------------------|", - "Password Manager App |", - "---------------------|", - "1. Create password |", - "2. Check password |", - "3. Edit password |", - "4. Delete password |", - "5. Exit |", - "---------------------|" - ] - print("\n".join(menu)) - return input("Pick a number: ") + try: + menu = [ + "---------------------|", + "Password Manager App |", + "---------------------|", + "1. Create password |", + "2. Check password |", + "3. Edit password |", + "4. Delete password |", + "5. Exit |", + "---------------------|" + ] + print("\n".join(menu)) + return input("Pick a number: ") + except Exception as e: + print(f"An error occurred: {e}") + return + def validate_aim(aim): """Validate if the aim is not empty.""" - if not aim: - display_message("Aim unspecified or empty. Please specify an 'aim' value.", Fore.RED) + try: + if not aim: + display_message("Aim unspecified or empty. Please specify an 'aim' value.", Fore.RED) + return False + return True + except Exception as e: + print("Exception occured: ", e) return False - return True def read_csv_file(): """Read the CSV file and return the data as a list of dictionaries.""" - if not os.path.isfile(PASSWORD_FILE): + try: + if not os.path.isfile(PASSWORD_FILE): + return [] + with open(PASSWORD_FILE, "r") as csvfile: + return list(csv.DictReader(csvfile)) + except Exception as e: + print("Exception occured: ", e) return [] - with open(PASSWORD_FILE, "r") as csvfile: - return list(csv.DictReader(csvfile)) def write_csv_file(data): """Write the data to the CSV file.""" - with open(PASSWORD_FILE, "w", newline="") as csvfile: - writer = csv.DictWriter(csvfile, fieldnames=["aim", "password"]) - writer.writeheader() - writer.writerows(data) + try: + with open(PASSWORD_FILE, "w", newline="") as csvfile: + writer = csv.DictWriter(csvfile, fieldnames=["aim", "password"]) + writer.writeheader() + writer.writerows(data) + except Exception as e: + print("Exception occured: ", e) + return def password_generator(): """Generate a random password with lowercase, uppercase, punctuation, and numeric characters.""" - char_sets = [string.ascii_lowercase, string.ascii_uppercase, string.punctuation, string.digits] - password = [random.choice(char_set) for char_set in char_sets] - password.extend(random.choices("".join(char_sets), k=STRING_LENGTH + PUNCTUATION_LENGTH + 4 - len(char_sets))) - random.shuffle(password) - return "".join(password) + try: + char_sets = [string.ascii_lowercase, string.ascii_uppercase, string.punctuation, string.digits] + password = [random.choice(char_set) for char_set in char_sets] + password.extend(random.choices("".join(char_sets), k=STRING_LENGTH + PUNCTUATION_LENGTH + 4 - len(char_sets))) + random.shuffle(password) + return "".join(password) + except Exception as e: + print("An error occurred: ", e) + def display_message(message, color=Fore.GREEN): """Display a message in the specified color."""