Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions projects/final-project/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
home = {

"bedroom": [],

"kitchen": [],

"bathroom": [],

"sitting room": [],

"wardrobe": []
}
9 changes: 9 additions & 0 deletions projects/final-project/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from user_interface import user_interface


def main():
print(user_interface())


if __name__ == '__main__':
main()
146 changes: 146 additions & 0 deletions projects/final-project/manage_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from typing import Any
from data import home


def add_object(room: list, name: str, category: str, position: str) -> str:
"""
Adds a new item to the room list

param room: the room in which there are objects
param name: object name
param category: object category
param position: object position
return: informs the user that the object has been added to the room
"""

room.append(
{
"name": name,
"category": category,
"position": position
}
)
return f"{name} added!"


def check_object_in_data(object_to_identify: str, room: list) -> list | str | Any:
"""
Checks if there are objects with that name in that room.

param object_to_identify: object name
param room: the room in which there are objects
return: an empty list if the function doesn't find the object in the room, otherwise returns the object name
"""

if not room:
return room

if object_to_identify == room[0]["name"]:
return room[0]

else:
return check_object_in_data(object_to_identify, room[1:])


def display_object(room: list) -> str:
"""
Allows to display all objects in the room

param room: the room in which there are objects
return: the objects in that room
"""
if not room:
return ""
else:
return str(room[0]) + "\n\n" + display_object(room[1:])


def delete_object(object_to_delete: str, room: list) -> str:
"""
Allows to remove an object in the room

param object_to_delete: object name
param room: the room in which there are objects
return: informs the user that the object has been deleted to the room
"""
if room[0]["name"] == object_to_delete:
room[0].clear()
room.remove({})
return "deleted!"
else:
delete_object(object_to_delete, room[1:])
room.remove({})
return "deleted!"


def update_object(value_name: str, object_to_update: str, room: list, new_value_name: str) -> str:
"""
Allows to update the value of an object-specific key

param value_name: the value to replace
param object_to_update: object name
param room: the room in which there are objects
param new_value_name: the value updated
return: informs the user that the object has been updated to the room
"""

if room[0]["name"] == object_to_update:
room[0][value_name] = new_value_name
return "Updated!"

else:
update_object(value_name, object_to_update, room[1:], new_value_name)
return "Updated!"


def find_room(object_name: str) -> list:
"""
Allows you to search the room in which the object is present

param object_name: object name
return: the room in which the object is present
"""

for rooms in home:

if check_object_in_data(object_name, home[rooms]):
room = rooms
return room


def display_type_of_category() -> set:
"""
Allows to display all objects categories

return: all categories of existing objects
"""

categories = set()

for room in home:
n = 0

while n < len(home[room]):
categories.add(home[room][n]["category"])
n += 1

return categories


def check_for_category(category: str, room: list) -> str:
"""
Allows you to search and return all objects by categories

param category: object category
param room: the room in which there are objects
return: all objects by categories
"""

if not room:
return ""

if category == room[0]["category"]:
return str(room[0]) + "\n" + check_for_category(category, room[1:])

else:
return check_for_category(category, room[1:])
136 changes: 136 additions & 0 deletions projects/final-project/user_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from data import home
from manage_data import (find_room, check_object_in_data, check_for_category, display_object, delete_object,
display_type_of_category, update_object, add_object)


def user_interface() -> str:
"""
This function represents the user interface for navigating home environments.
Users can search for specific items, manage objects in their environment, and explore various rooms.

return: an information string once asked to close the program
"""

print("Welcome home!!!")

using_the_app = input("\nPress Enter if you want to use the app. Otherwise press 'q' to exit: ")
while using_the_app != "" and using_the_app.lower() != "q":
using_the_app = input("\nPress Enter if you want to use the app. Otherwise press 'q' to exit: ")

while using_the_app == "":

checking_object = input("Are you looking for a specific item?\nPress Enter to write its name and search for it,"
" otherwise press 'q' to explore the various environments:\n")
while checking_object != "" and checking_object.lower() != "q":
checking_object = input("Are you looking for a specific item?\nPress Enter to write its name and search "
"for it, otherwise press 'q' to explore the various environments:\n")

if checking_object == "":
object_to_find = input("Enter the name of the object: ").lower()
room = find_room(object_to_find)

if room:
print(f"Your object is in the {room}")
print(check_object_in_data(object_to_find, home[room]))

else:
print(f"{object_to_find} not found!")
print("Would you prefer searching by categories?")
checking_categories = input(
"Press Enter if you want to find it by categories, or press 'q' to explore your environment: ")

while checking_categories != "" and checking_categories.lower() != "q":
checking_categories = input(
"Press Enter if you want to find it by categories, or press 'q' to explore your environment: ")

if checking_categories == "":
print("Here are the currently updated categories: ")
categories = display_type_of_category()

for i in categories:
print(str(i))
category = input("Choose one of the following categories:").lower()

if category not in categories:
print(f"{category} not found!")

else:

for i in home:
print(check_for_category(category, home[i]))

print("Explore one of your environments:\n")

for key in home:
print(key)

user_room = input("Type here the name of your environment: ").lower()
while user_room not in home:
print(f"{user_room} not found!")
user_room = input("Type here the name of your environment: ").lower()

user_choice = input("\nPress Enter if you want to display your items,\n"
"press 'a' to add an item to your environment,\n"
"press m to manage your object,\n"
"press r to delete your object,\n"
"press q to quit: ")
while (user_choice.lower() != "" and user_choice.lower() != "a" and user_choice.lower() != "m"
and user_choice.lower() != "r" and user_choice.lower() != "q"):
user_choice = input("\nPress Enter if you want to display your items,\n"
"press 'a' to add an item to your environment,\n"
"press m to manage your object,\n"
"press r to delete your object,\n"
"press q to quit: ")

if user_choice == "":
display = display_object(home[user_room])

if display:
print(display_object(home[user_room]))

else:
print("Empty environment!")

if user_choice.lower() == "a":
name = input("Enter the name of the object: ").lower()
check_name = check_object_in_data(name, home[user_room])

if not check_name:
category = input("Enter the type of category of the object: ").lower()
position = input("Enter where your object is located: ").lower()
print(add_object(home[user_room], name, category, position))

else:
print(f"{name} already in the environment!")

if user_choice.lower() == "m":
object_to_update = input("Enter the name of the object to update: ").lower()

if check_object_in_data(object_to_update, home[user_room]):
name = input("Enter the new object name: ").lower()
print(update_object("name", object_to_update, home[user_room], name))
category = input("Enter the type of category of the object: ").lower()
print(update_object("category", name, home[user_room], category))
position = input("Enter where your object is located: ").lower()
print(update_object("position", name, home[user_room], position))

else:
print(f"{object_to_update} not found!")

if user_choice.lower() == "r":
object_to_delete = input("Enter the name of the object to delete: ").lower()

if check_object_in_data(object_to_delete, home[user_room]):
print(delete_object(object_to_delete, home[user_room]))

else:
print(f"{object_to_delete} not found!")

if user_choice.lower() == "q":
break

using_the_app = input("\nPress Enter if you want to use the app. Otherwise press 'q' to exit: ")
while using_the_app != "" and using_the_app.lower() != "q":
using_the_app = input("\nPress Enter if you want to use the app. Otherwise press 'q' to exit: ")

return "Closing the app"