Question

Object-Oriented Design
In this Activity, you will write Python code for part of your cumulative project app idea. With your design in mind, think about what classes your app could have. Choose one class and code it in PythonAnywhere.
Your class must include at least one class attribute
You must demonstrate the use of local and global variables
Test your program by instantiating several objects and demonstrate the methods you’ve written
Make sure to use proper spacing, indentation, and include comments to document your program, ensuring displayed output includes descriptive identifiers or labels for readability.
Submit your code by clicking on the Share button PythonAnywhere. Copy the link, paste it into answer space below.

Answers

GPT-4o mini
Sure! Below is an example of a Python class that follows the guidelines you've provided. Let’s create a simple class for a **Library** that manages books. The class has a class attribute to track the total number of books in the library, local variables within method definitions, and global variables for demonstration.

### Code Example

```python
class Library:
# Class attribute to keep track of the total number of books in the library
total_books = 0

def __init__(self, name):
# Instance attribute for the library name
self.name = name
# Instance attribute to store books in the library
self.books = []

def add_book(self, book_name):
"""
Adds a new book to the library
:param book_name: The name of the book to be added
"""
# Using a local variable for the book
if book_name not in self.books:
self.books.append(book_name)
Library.total_books += 1 # Incrementing the class attribute
print(f"Added '{book_name}' to the library '{self.name}'.")

def display_books(self):
"""
Displays all the books in the library
"""
# Using a local variable to track book count
local_count = len(self.books)
print(f"Library '{self.name}' contains the following books ({local_count} total):")
for book in self.books:
print(f"- {book}")

# Global variable to hold the library instances
libraries = []

# Creating instances of the Library class
library1 = Library("Main City Library")
library2 = Library("Community Library")

# Adding books to the libraries
library1.add_book("The Great Gatsby")
library1.add_book("1984")
library2.add_book("To Kill a Mockingbird")
library2.add_book("Moby Dick")

# Displaying books in each library
library1.display_books()
library2.display_books()

# Displaying the total number of books across all libraries
print(f"Total number of books in all libraries: {Library.total_books}")
```

### Explanation of the Code
1. **Class Attribute**: `total_books` is a class attribute that keeps track of how many books have been added across all instances of the `Library`.

2. **Instance Attributes**: Each library has its own `name` and a list of `books`.

3. **Local and Global Variables**:
- Local variables like `local_count` within the `display_books` method help count the number of books in the local context.
- A global variable `libraries` exists for demonstration purposes but is not used in this example.

4. **Method Demonstration**: The code creates two library instances, adds books to them, and displays the books along with the total count of books across libraries.

### Testing the Program
You can run the code to see how it organizes the library and outputs the necessary details for the added books. The printed output is well-labeled for readability.

### PythonAnywhere
To share this code using PythonAnywhere, you would simply need to navigate to your file, click on the "Share" button, and copy the link to share with others.

You can use this link setup to show your project to your instructor or classmates. If you need further assistance in posting this on PythonAnywhere or any adjustments to expand this example, let me know!

Related Questions