Reputation: 65
The assignment I'm working on requests the following:
"Display all tasks in a manner that is easy to read. Make sure that each task is displayed with a corresponding number which can be used to identify the task."
I'm trying to add a variable called 'tasknum' that automatically loops through the preceding lines of the text file and increments the most recent value by one. In other words, counts how many lines (ie tasks) are already present, adds 1 and writes that new value to the tasknum variable in the task being added.
I'm unable to get the variable to increment. It stays on its initial value of 0 or seems to be skipping the for loop completely.
How can I make sure each new added line contains the variable tasknum with a value of +1 of the previous line?
tasks.txt contents:
admin, 1, Register Users, Add the usernames and passwords for all team members, 10 Oct 2019, 20 Oct 2019, No
admin, 2, Assign initial tasks, Assign each team member with appropriate tasks, 10 Oct 2019, 25 Oct 2019, No
My code thus far:
def add_task():
tasklist = open("tasks.txt", "a+")
# Requests user input to add tasks to list
user = input("Please enter your username\n")
tasknum = 0
tasktitle = input("Please enter the task title\n")
description = input("Please enter the task description\n")
dateadded = input("Please enter today's date\n")
datedue = input("Please enter the date this task is due\n")
status = "No"
# Task counter: increments task number
for tasknum in tasklist:
tasklist[1] = tasknum
tasknum += 1
# Writes user input to file and displays thank you message
tasklist.write(f"\n{user}, {tasknum}, {tasktitle}, {description}, {dateadded}, {datedue}, {status}")
print("Thanks, your task has been added")
tasklist.close()
if user == "admin" and login == True:
admin_menu()
else:
user_menu()
Upvotes: 0
Views: 365
Reputation: 318
The problem seems to be, that you are trying to index your file like a list, which does not work. You will first need to read all your lines and create a list from them like so:
lines = [line for line in tasklist]
Note that you only have each line as a list item i.e. [line1, line2, ...], rather than a nested list [[line1 word1, line1 word2, ...], [line2 word1, line2 word2, ...], ...]. This would easily be achievable but is not necessary since you only need the next number. You can get this by taking the len(lines) + 1. Note that I also changed your open mode to r+ as a+ did not correctly read the lines.
Full code example:
def add_task():
tasklist = open("tasks.txt", "r+")
# Requests user input to add tasks to list
user = input("Please enter your username\n")
tasktitle = input("Please enter the task title\n")
description = input("Please enter the task description\n")
dateadded = input("Please enter today's date\n")
datedue = input("Please enter the date this task is due\n")
status = "No"
# Task counter: increments task number
lines = [line for line in tasklist] # Read all lines
tasknum = len(lines)+1 # Get number of lines and increment by 1
# Writes user input to file and displays thank you message
tasklist.write(f"\n{user}, {tasknum}, {tasktitle}, {description}, {dateadded}, {datedue}, {status}")
print("Thanks, your task has been added")
tasklist.close()
if user == "admin" and login == True:
admin_menu()
else:
user_menu()
Edit: In case you do want to read the tasknum from your last line rather than counting your lines, you can do this:
def add_task():
tasklist = open("tasks.txt", "r+")
# Requests user input to add tasks to list
user = input("Please enter your username\n")
tasknum = 0
tasktitle = input("Please enter the task title\n")
description = input("Please enter the task description\n")
dateadded = input("Please enter today's date\n")
datedue = input("Please enter the date this task is due\n")
status = "No"
# Task counter: increments task number
lines = [line.split(", ") for line in tasklist] # Read all lines splitting on comma and whitespace
last_line = lines[-1] # Get last line
tasknum = int(last_line[1]) # Get tasknum from last_line and convert to int
tasknum += 1 # Increment tasknum
# Writes user input to file and displays thank you message
tasklist.write(f"\n{user}, {tasknum}, {tasktitle}, {description}, {dateadded}, {datedue}, {status}")
print("Thanks, your task has been added")
tasklist.close()
if user == "admin" and login == True:
admin_menu()
else:
user_menu()
Upvotes: 1
Reputation: 1
You dont need to increment the index manually the for loops does it automatically in python with each time it loops
Upvotes: 0