Hi everyone, this week I've been focusing on improving my skills in Variables and Data Types, control flow, modular programming, OOP, packages, and modules. I've gained a deeper understanding of these concepts and I believe that the best way to solidify my understanding is by putting it into practice. To do this, I plan on working on some mini projects that will allow me to revisit and reinforce what I've learned this week.
The following is an example of implementing a simple Todo List application using OOP concepts and control flow in Python
class Todo:
def __init__(self, task):
self.task = task
self.is_done = False
def __str__(self):
return self.task + (' (Done)' if self.is_done else ' (Not done)')
def mark_as_done(self):
self.is_done = True
class TodoList:
def __init__(self):
self.todos = []
def add_task(self, task):
self.todos.append(Todo(task))
def mark_as_done(self, task):
for todo in self.todos:
if todo.task == task:
todo.mark_as_done()
def __str__(self):
return '\n'.join(str(todo) for todo in self.todos)
if __name__ == '__main__':
todo_list = TodoList()
while True:
print('[1] Add task')
print('[2] Mark task as done')
print('[3] Quit')
choice = int(input('Choose an option: '))
if choice == 1:
task = input('Enter task: ')
todo_list.add_task(task)
elif choice == 2:
task = input('Enter task: ')
todo_list.mark_as_done(task)
elif choice == 3:
break
else:
print('Invalid option.')
print(todo_list)
Here is an explanation of where the concepts of OOP (Object-Oriented Programming) and control flow are used in the simple Todo List application.
The concept of OOP is used in the Todo List application through the use of classes and objects. There are two classes, Todo
and TodoList
. The Todo
class represents an individual task and has two attributes: task
(a string that represents the task) and is_done
(a boolean that determines whether the task is complete or not). The Todo
class also has a mark_as_done
method which changes the is_done
attribute to True
.
The TodoList
class represents a list of tasks
and has one attribute: todos
(a list of Todo objects). The TodoList
class also has three methods: add_task
, which adds a new task
to the list
; mark_as_done
, which marks a task as complete; and __str__
, which returns a string representation of the list of tasks.
The concept of control flow
is used in the Todo List application through the use of a while
loop and user input. The program will continue to loop as long as the user has not chosen the option to exit. Each time the program loops, the user can choose to add a task
, mark a task as complete, or exit the application. Based on the selected option, the program will call the appropriate method from the TodoList
object.
Creating a Simple Login and Register System in Python with Modular Programming and OOP Concepts
Code :
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.is_logged_in = False
def login(self, entered_username, entered_password):
if entered_username == self.username and entered_password == self.password:
self.is_logged_in = True
return True
return False
def logout(self):
self.is_logged_in = False
class UserManager:
def __init__(self):
self.users = []
def register(self, username, password):
user = User(username, password)
self.users.append(user)
def login(self, username, password):
for user in self.users:
if user.login(username, password):
return user
return None
user_manager = UserManager()
def register():
print("Register")
print("--------")
username = input("Enter username: ")
password = input("Enter password: ")
user_manager.register(username, password)
print("Successfully registered.")
def login():
print("Login")
print("-----")
username = input("Enter username: ")
password = input("Enter password: ")
user = user_manager.login(username, password)
if user:
print("Successfully logged in.")
user.is_logged_in = True
else:
print("Login failed.")
def logout(user):
user.logout()
print("Successfully logged out.")
def show_menu():
print("Menu")
print("----")
print("1. Register")
print("2. Login")
print("3. Logout")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
register()
elif choice == 2:
login()
elif choice == 3:
logout(user)
elif choice == 4:
exit()
while True:
show_menu()
Here is an explanation of the code
In the code, the concept of modular programming is used by separating the user registration
logic and login
logic into two functions, register
and login
. The OOP concept is used through the use of the User
and UserManager
classes. The User class represents a user and has attributes username
, password
, and is_logged_in
. The User class also has the login method to verify the entered username
and password
and the logout method to log out of the application. The UserManager
class represents the user manager and has the attribute users (a list of User objects) and the register method to add a User object to the list
of users
and the login
method to verify the entered username and password.
In the code
, the concept of control flow
is also applied using a while
loop and selection
(if-elif-else) to determine what the application will do based on the user's input. In this case
, the show_menu
function will display the menu and request input from the user, then call the appropriate
function based on the user's
input.
Top comments (0)