0% found this document useful (0 votes)
2 views1 page

Add Function

The document outlines a function to add tasks to a list, ensuring that all required fields are present and valid. It checks for duplicate task IDs, valid priority levels, and acceptable statuses before appending the task to the list. If any validation fails, it raises a ValueError with an appropriate message.

Uploaded by

mo99agha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Add Function

The document outlines a function to add tasks to a list, ensuring that all required fields are present and valid. It checks for duplicate task IDs, valid priority levels, and acceptable statuses before appending the task to the list. If any validation fails, it raises a ValueError with an appropriate message.

Uploaded by

mo99agha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

tasks = []

REQUIRED_FIELDS = ["task_id", "title", "priority", "status"]


VALID_PRIORITIES = ["Low", "Medium", "High"]
VALID_STATUSES = ["To Do", "In Progress", "Done"]

def add_task(task):
for field in REQUIRED_FIELDS:
if field not in task:
raise ValueError(f"Missing required field: {field}")

for existing_task in tasks:


if existing_task["task_id"] == task["task_id"]:
raise ValueError(f"Task ID {task['task_id']} already exists.")

if task["priority"] not in VALID_PRIORITIES:


raise ValueError(f"Invalid priority: {task['priority']}. Must be one of
{VALID_PRIORITIES}")

if task["status"] not in VALID_STATUSES:


raise ValueError(f"Invalid status: {task['status']}. Must be one of
{VALID_STATUSES}")

tasks.append(task)
print(f"Task '{task['title']}' added successfully! ")

You might also like