Day 051 - Project 51 : ToDo list with saving

@QwertyQwerty88 I don’t know what to do next, here’s my code:

import os, time

running = True
toDo = [ ]

f = open("toDo.baldy", "r")
toDo = eval(f.read())
f.close()

def remove():
  remove=input("What do you want to remove? ").upper()
  for row in toDo:
    if remove in row:
      toDo.remove(row)
    else:
      print("That item is not in the 'ToDo'")
      os.system("clear")
      
def edit():
  edit=input("What is the name of the 'Todo' you want to edit? ").upper()
  found=False
  for row in toDo:
    if edit in row:
      found=True
    if not found:
      print("That is not in the ToDo")
      return
    for row in toDo:
      if edit in row:
        toDo.remove(row)
        add = input("What do you want to add? ").upper()
        due = input("When is it due by?")
        priority = input("High, medium or low priority? (a or b or c)").upper()
        if priority == "A":
          priority = "HIGH"
        elif priority == "B":
          priority = "MEDIUM"
        elif priority == "C":
          priority = "LOW"
        row=[add, due, priority]
        toDo.append(row)
        print("ToDo has been edited.")
    
def view():
  view = input(
    "Do you want to view all or view a certain priority? (a or b)").upper()
  if view == "A":
    prettyPrint()
  if view == "B":
    viewpriority = input(
      "What priority do you want to view?").upper()
    for row in toDo:
      if viewpriority in row:
        for item in row:
          print(item, end=" | ")
        print()
    print()
  time.sleep(1)
      
def add():
  add = input("What do you want to add? ").upper()
  due = input("When is it due by?")
  priority = input("High, medium or low priority? (a,b or c)").upper()
  if priority == "A":
    priority = "HIGH"
  elif priority == "B":
    priority = "MEDIUM"
  elif priority == "C":
    priority = "LOW"
  row = [add, due, priority]
  toDo.append(row)
  print("This 'ToDo' has been added")

def prettyPrint():
  for row in toDo:
    for item in row:
      print(f"{item:^10} |",end="")
    print()
  print()

while running:
  menu = input("Do you want to add, view, remove, or edit a 'to do'? ").upper()
  if menu == "ADD":
    add()
    time.sleep(2)
    os.system("clear")
  elif menu == "VIEW":
    view()
    time.sleep(2)
    os.system("clear")
  elif menu=='REMOVE':
    remove()
    print("Deleting...")
    time.sleep(2)
    os.system("clear")
  elif menu=='EDIT':
    edit()
    time.sleep(2)
    os.system("clear")

Okay, you’re loading from the toDo.baldy file, but not saving to it. So when you append to it, you need to write to the toDo.baldy file.

What do you mean by writing to the toDo.baldy file?

Okay, you read from the toDo.baldy file, right? That gives you an empty list. When you append to the toDo variable, you’re not saving to the file, but saving to the list. But every time you rerun the program, that toDo variable just gets set back to that same empty list. So you need to modify the toDo.baldy file.

Basically, use the write method to replace the current text in the file.

with open("toDo.baldy", "w") as file:
  file.write(toDo)

Now every time you edit the toDo list, use this code to update the file.

@QwertyQwerty88 Where do I add it?

As I said before,

(btw that code might be wrong Idk I barely use txt files with python lol)

@QwertyQwerty88 Now I get an error…

import os, time

running = True
toDo = []


f = open("toDo.baldy", "r")
toDo = eval(f.read())
f.close()

def remove():
  remove=input("What do you want to remove? ").upper()
  for row in toDo:
    if remove in row:
      toDo.remove(row)
    else:
      print("That item is not in the 'ToDo'")
      os.system("clear")
      
def edit():
  edit=input("What is the name of the 'Todo' you want to edit? ").upper()
  found=False
  for row in toDo:
    if edit in row:
      found=True
    if not found:
      print("That is not in the ToDo")
      return
    for row in toDo:
      if edit in row:
        toDo.remove(row)
        add = input("What do you want to add? ").upper()
        due = input("When is it due by?")
        priority = input("High, medium or low priority? (a or b or c)").upper()
        if priority == "A":
          priority = "HIGH"
        elif priority == "B":
          priority = "MEDIUM"
        elif priority == "C":
          priority = "LOW"
        row=[add, due, priority]
        toDo.append(row)
        print("ToDo has been edited.")
        with open("toDo.baldy", "w") as file:
          file.write(toDo)
    
def view():
  view = input(
    "Do you want to view all or view a certain priority? (a or b)").upper()
  if view == "A":
    prettyPrint()
  if view == "B":
    viewpriority = input(
      "What priority do you want to view?").upper()
    for row in toDo:
      if viewpriority in row:
        for item in row:
          print(item, end=" | ")
        print()
    print()
  time.sleep(1)
      
def add():
  add = input("What do you want to add? ").upper()
  due = input("When is it due by?")
  priority = input("High, medium or low priority? (a,b or c)").upper()
  if priority == "A":
    priority = "HIGH"
  elif priority == "B":
    priority = "MEDIUM"
  elif priority == "C":
    priority = "LOW"
  row = [add, due, priority]
  toDo.append(row)
  print("This 'ToDo' has been added")

def prettyPrint():
  for row in toDo:
    for item in row:
      print(f"{item:^10} |",end="")
    print()
  print()

while running:
  menu = input("Do you want to add, view, remove, or edit a 'to do'? ").upper()
  if menu == "ADD":
    add()
    time.sleep(2)
    os.system("clear")
  elif menu == "VIEW":
    view()
    time.sleep(2)
    os.system("clear")
  elif menu=='REMOVE':
    remove()
    print("Deleting...")
    time.sleep(2)
    os.system("clear")
  elif menu=='EDIT':
    edit()
    time.sleep(2)
    os.system("clear")
    
#  f = open("toDo.baldy", "w") # Permissions set to 'w' because we are deleting the file and replacing it with the whole 2D list every time.
#  f.write(str(toDo)) # Need to cast the list to a single string
#  f.close()

Alright, I have to go to bed now… Bye

K, what is the error? Can you paste it here?

2 Likes

Screenshot 2023-08-16 080318

nvm I figured it out

Queen Sew QR Code - Replit Can you reset it?

Reset what? That’s not even Python…

Dear All, in my view there is an error in the courses’ code:

python
myEvents = []

f = open("calendar.txt","r")
myEvents = eval(f.read())
f.close()

def prettyPrint():
  print()
  for row in myEvents:
    print(f"{row[0] :^15} {row[1] :^15}")
  print()

while True:
  menu = input("1: Add, 2: Remove\n")

  if menu == "1":
    event = input("What event?: ").capitalize()
    date = input("What date?: ")
    row = [event,date]
    myEvents.append(row)
    prettyPrint()

  else:
    criteria = input("What event do you want to remove?: ").title()
    for row in myEvents:
      if criteria in row:
        myevents.remove(row)

  
f = open("calendar.txt", "w") 
f.write(str(myEvents)) 
f.close()

It says

criteria = input("What event do you want to remove?: ").title()

but it should be

criteria = input("What event do you want to remove?: ").capitalize()
1 Like