Impress me with your coding skills
- User
Impress me with your coding skills
Assistant
Certainly! Here's an example of a Python script that demonstrates a variety of coding skills, including data structures, algorithms, and best practices. This script will create a simple command-line address book application.
import json class AddressBook: def __init__(self): self.contacts = {} def add_contact(self, name, phone, email): self.contacts[name] = {'phone': phone, 'email': email} print(f"Contact {name} added.") def delete_contact(self, name): if name in self.contacts: del self.contacts[name] print(f"Contact {name} deleted.") else: print(f"Contact {name} not found.") def display_contacts(self): if not self.contacts: print("No contacts available.") else: for name, details in self.contacts.items(): print(f"Name: {name}, Phone: {details['phone']}, Email: {details['email']}") def save_to_file(self, filename): with open(filename, 'w') as file: json.dump(self.contacts, file) print(f"Address book saved to {filename}.") def load_from_file(self, filename): try: with open(filename, 'r') as file: self.contacts = json.load(file) print(f"Address book loaded from {filename}.") except FileNotFoundError: print(f"File {filename} not found.") def main(): address_book = AddressBook() while True: print("\nAddress Book Menu:") print("1. Add Contact") print("2. Delete Contact") print("3. Display Contacts") print("4. Save to File") print("5. Load from File") print("6. Exit") choice = input("Enter your choice: ") if choice == '1': name = input("Enter name: ") phone = input("Enter phone: ") email = input("Enter email: ") address_book.add_contact(name, phone, email) elif choice == '2': name = input("Enter name to delete: ") address_book.delete_contact(name) elif choice == '3': address_book.display_contacts() elif choice == '4': filename = input("Enter filename to save: ") address_book.save_to_file(filename) elif choice == '5': filename = input("Enter filename to load: ") address_book.load_from_file(filename) elif choice == '6': print("Exiting...") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()Key Features:
- Object-Oriented Design: The
AddressBookclass encapsulates the functionality related to managing contacts. - Data Persistence: Contacts can be saved to and loaded from a JSON file.
- User Interface: A simple command-line interface allows users to interact with the address book.
- Error Handling: Basic error handling for file operations.
- Code Organization: Clear separation of concerns between the
AddressBookclass and themainfunction.
This script showcases good coding practices, including:
- Use of classes for encapsulation.
- Input validation and error handling.
- File I/O operations for data persistence.
- A simple yet effective user interface.
Feel free to test and extend this script!
- Object-Oriented Design: The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.