Skip to content
Open
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
58 changes: 50 additions & 8 deletions python/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,58 @@

TASK_FILE = ".tasks.txt"


Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try not to add additional whitespace in a function which is not the subject of the commit. Keep commits clean and use git diff to see what will change in each file.

def add_task(task):
"""Function: add_task

Input - a task to add to the list
Return - nothing
"""
#Open the file in append mode to add a new task to the end of the exisitng list
with open(TASK_FILE, "a", encoding="utf-8") as file:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect

#Write the task followed by a newline character so the task appears on a new line
file.write(task +"\n")

#Listing all tasks within the task file
def list_tasks():
return

with open(TASK_FILE, "r", encoding="utf-8") as file:
tasks = file.readlines()
counter = 1
output_string = ""
for task in tasks:
output_string = output_string + str(counter) + ". " +task
counter = counter + 1
#Code from Gemini
my_string = '1. Item 1\n2. Item 2\n3. Item 3\n4. Item 4\n5. Item 5\n'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost; you have output_string correct, so this line should be removed and the line below is output_string[:-1], or better yet output_string.rstrip()

modified_string = my_string[:-1]
print(modified_string)

return modified_string


def remove_task(index):
return
"""Function: remove_task

Input - a task to add to the list
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super! Some errors in the syntax here, with a random else. The if accompanying this has gone awol...

Return - nothing
"""
#Read all lines within the task file
with open(TASK_FILE, 'r', encoding='utf-8') as file:
lines = file.readlines()

#Check that the line to remove is valid
if 1 <= index <= len(lines):
lines.pop(index - 1)

#Update the list and add it back to the original file
with open(TASK_FILE, 'w', encoding='utf-8') as file:
file.writelines(lines)


print ('Task removed.')
else:
print('Invalid task number.')

return



def main():
parser = argparse.ArgumentParser(description="Command-line Todo List")
Expand All @@ -42,7 +81,10 @@ def main():
tasks = list_tasks()
print(tasks)
elif args.remove:
remove_task(int(args.remove))
try: remove_task(int(args.remove))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good attempt at this!

#Code from Gemini
except ValueError:
print("Error: Please provide a number for the task index to remove.")
else:
parser.print_help()

Expand Down