'I am trying to display the logged in users tasks in the portal, but program displays 'local variable 'username' referenced before assignment'
My code so far is:
def view_mine():
with open("tasks.txt", 'r') as g:
lines = g.readlines()
user_tasks = []
for line in lines:
try:
line = line.split(',')
if line[1] == username.strip():
user_tasks.append(line)
except IndexError:
continue
for line in user_tasks:
username = line[0]
task = line[1]
task_description = line[2]
task_assigned = line[3]
due_date = line[4]
task_completion = line[5]
print("Task username: ", username,
'\nTask: ', task,
'\nTask Description: ', task_description,
'\nDate Assigned: ', task_assigned,
'\nTask Due Date: ', due_date,
'\nTask Completed: ', task_completion)
break
I have tried the "global username", but it also brings up an error code where the name 'username' is not defined. Any help would be appreciated!
Solution 1:[1]
You are using the variable username on the for line in lines: loop without having created it. Try getting its values with username = ... before calling that loop, so it will be assigned before being used, and your code will work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cardstdani |
