diff --git a/basic_file_encryptor.py b/basic_file_encryptor.py new file mode 100644 index 00000000..20a64ab4 --- /dev/null +++ b/basic_file_encryptor.py @@ -0,0 +1,61 @@ +def caesar_cipher(text, shift, encrypt=True): + """ + Encrypts or decrypts the input text using the Caesar cipher. + + Parameters: + - text: The input text to encrypt or decrypt. + - shift: The number of positions to shift each character (the key). + - encrypt: Specifies the operation; True for encryption, False for decryption. + + Returns: + - The encrypted or decrypted text. + """ + result = "" + for char in text: + if char.isalpha(): # Check if the character is an alphabet + start = 'A' if char.isupper() else 'a' + # Shift the character + result += chr((ord(char) + (shift if encrypt else -shift) - ord(start)) % 26 + ord(start)) + else: + # Non-alphabetical characters remain the same + result += char + return result + +def process_file(filename, shift, encrypt=True): + """ + Encrypts or decrypts the contents of a file. + + Parameters: + - filename: The name of the file to process. + - shift: The number of positions to shift each character. + - encrypt: Specifies the operation; True for encryption, False for decryption. + """ + try: + with open(filename, 'r', encoding='utf-8') as file: + content = file.read() + + # Encrypt or decrypt the content + processed_content = caesar_cipher(content, shift, encrypt) + + # Save the processed content back to the file + with open(filename, 'w', encoding='utf-8') as file: + file.write(processed_content) + + print("Operation completed successfully.") + except FileNotFoundError: + print(f"The file {filename} was not found.") + except Exception as e: + print(f"An error occurred: {e}") + +def main(): + choice = input("Do you want to encrypt or decrypt a file? (e/d): ").lower() + if choice not in ['e', 'd']: + print("Invalid choice. Please enter 'e' for encrypt or 'd' for decrypt.") + return + + filename = input("Enter the filename: ") + shift = int(input("Enter the shift key (number): ")) + process_file(filename, shift, encrypt=(choice == 'e')) + +if __name__ == "__main__": + main() diff --git a/basic_quiz_game.py b/basic_quiz_game.py new file mode 100644 index 00000000..28242304 --- /dev/null +++ b/basic_quiz_game.py @@ -0,0 +1,37 @@ +# Basic Quiz Game in Python + +def start_quiz(questions): + score = 0 + for question, options, correct_answer in questions: + print(question) + for i, option in enumerate(options, 1): + print(f"{i}. {option}") + answer = input("Enter your answer (1/2/3/4): ") + if answer.isdigit() and int(answer) == correct_answer: + print("Correct!") + score += 1 + else: + print("Wrong answer.") + print() # Blank line for readability between questions + + print(f"Quiz completed! Your score is {score}/{len(questions)}.") + +# List of questions. Each question is a tuple containing the question text, +# a list of options, and the index of the correct answer (1-based). +questions = [ + ("What is the capital of France?", ["1) London", "2) Paris", "3) Berlin", "4) Madrid"], 2), + ("What is 2 + 2?", ["1) 3", "2) 4", "3) 5", "4) 6"], 2), + ("Who wrote Hamlet?", ["1) Tolkien", "2) Shakespeare", "3) Austen", "4) Hemingway"], 2), + ("Which gas is most abundant in the Earth's atmosphere?", ["1) Oxygen", "2) Hydrogen", "3) Carbon Dioxide", "4) Nitrogen"], 4), + ("What is the largest planet in our solar system?", ["1) Earth", "2) Mars", "3) Jupiter", "4) Saturn"], 3), + ("In what year did the Titanic sink?", ["1) 1912", "2) 1905", "3) 1898", "4) 1923"], 1), + ("Which element has the chemical symbol 'O'?", ["1) Gold", "2) Oxygen", "3) Osmium", "4) Olmium"], 2), + ("Who is known as the father of computer science?", ["1) Albert Einstein", "2) Isaac Newton", "3) Charles Babbage", "4) Alan Turing"], 3), + ("What is the hardest natural substance on Earth?", ["1) Diamond", "2) Quartz", "3) Gold", "4) Iron"], 1), + ("Which country is known as the Land of the Rising Sun?", ["1) China", "2) Australia", "3) Japan", "4) South Korea"], 3), +] + + +if __name__ == "__main__": + print("Welcome to the Basic Quiz Game!") + start_quiz(questions) diff --git a/expense_tracker.py b/expense_tracker.py new file mode 100644 index 00000000..431193b4 --- /dev/null +++ b/expense_tracker.py @@ -0,0 +1,58 @@ +import json +from datetime import datetime + +# Expense Tracker CLI Application + +# File to store the expense data +DATA_FILE = 'expenses.json' + +# Load expenses from the file +def load_expenses(): + try: + with open(DATA_FILE, 'r') as file: + return json.load(file) + except FileNotFoundError: + return [] + +# Save expenses to the file +def save_expenses(expenses): + with open(DATA_FILE, 'w') as file: + json.dump(expenses, file, indent=4) + +# Add a new expense +def add_expense(expenses): + category = input("Enter the expense category: ") + amount = float(input("Enter the expense amount: ")) + description = input("Enter the expense description: ") + date = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + expenses.append({"category": category, "amount": amount, "description": description, "date": date}) + save_expenses(expenses) + print("Expense added successfully.") + +# List all expenses +def list_expenses(expenses): + for expense in expenses: + print(f"Category: {expense['category']}, Amount: {expense['amount']}, Description: {expense['description']}, Date: {expense['date']}") + +# Main function +def main(): + expenses = load_expenses() + while True: + print("\nExpense Tracker CLI") + print("1. Add an expense") + print("2. List all expenses") + print("3. Exit") + choice = input("Choose an option (1/2/3): ") + + if choice == '1': + add_expense(expenses) + elif choice == '2': + list_expenses(expenses) + elif choice == '3': + print("Exiting the Expense Tracker. Goodbye!") + break + else: + print("Invalid choice. Please enter 1, 2, or 3.") + +if __name__ == "__main__": + main() diff --git a/morse_code_translator.py b/morse_code_translator.py new file mode 100644 index 00000000..d76bf2e7 --- /dev/null +++ b/morse_code_translator.py @@ -0,0 +1,61 @@ +# Morse Code Translator in Python + +# Dictionary mapping letters and numbers to Morse code +MORSE_CODE_DICT = { + 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', + 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', + 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', + 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', + 'Y': '-.--', 'Z': '--..', + '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', + '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----' +} + +# Function to translate text to Morse code +def text_to_morse(text): + morse_code = '' + for char in text.upper(): + if char in MORSE_CODE_DICT: + morse_code += MORSE_CODE_DICT[char] + ' ' + elif char == ' ': + morse_code += ' ' # Separate words with double space + return morse_code + +# Function to translate Morse code to text +def morse_to_text(morse_code): + text = '' + morse_code += ' ' # Ensure the last code is processed + morse_char = '' + space_count = 0 + for char in morse_code: + if char != ' ': + space_count = 0 + morse_char += char + else: + space_count += 1 + if space_count == 1: # End of a character + if morse_char in MORSE_CODE_DICT.values(): + text += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT.values()).index(morse_char)] + morse_char = '' + elif space_count == 3: # End of a word + text += ' ' + return text + +# Main program +if __name__ == "__main__": + while True: + print("\nMorse Code Translator") + print("1. Text to Morse Code") + print("2. Morse Code to Text") + choice = input("Choose an option (1/2): ") + if choice == '1': + text = input("Enter text to translate: ") + print("Morse Code:", text_to_morse(text)) + elif choice == '2': + morse_code = input("Enter Morse code to translate: ") + print("Text:", morse_to_text(morse_code)) + else: + print("Invalid choice. Please enter 1 or 2.") + + if input("\nTranslate another? (y/n): ").lower() != 'y': + break