Day 051 - Project 51 : ToDo list with saving

The problem is that the first time the if fails else kicks in and the return stops everything,
You probably meant something like this instead

for row in todolist:
        if task in row:
            todolist.remove(row)
            print("\033[1;31mTask removed!")
            time.sleep(2)
            os.system('clear')
            return

print("\033[1;31mTask not in list")
time.sleep(2)
os.system('clear')
3 Likes

Thankyou for the correction!

1 Like

No problem. I hope the why of the error is clear (I have done this sort of error myself many times )

I cleared the list but even when I copy and paste some of the code, It says there was an unexpected EOF while parsing, and that there was an error on line zero

Please share the code od REPL so that we can help.

1 Like

Chat GPT has the following contribution to make regarding using the “eval()” function:

When considering the trade-off between using eval() and alternative methods like using split() or strip() to read and process the file content, there are a few factors to consider:

  1. Complexity: Using eval() can be a more concise approach since it evaluates the entire file content as a Python expression and assigns it directly to a variable. This can be advantageous if the file content represents a complex data structure, such as a nested list or dictionary. In contrast, using split() or strip() would require additional code to parse and process the file content manually.

  2. Readability and Maintainability: Using eval() can make the code less readable and maintainable, especially if the file format and content are not well-defined or if there is potential for ambiguity. Splitting the file content and manually processing it using appropriate functions can make the code more explicit and easier to understand.

  3. Security and Risk: eval() carries potential security risks if the file content is untrusted or can be modified by external sources. Using eval() without proper validation can lead to code execution vulnerabilities. On the other hand, parsing and processing the file content manually using safer methods like split() or strip() reduces the risk of unintended code execution.

Overall, the trade-off depends on the specific requirements and context of your project. If the file content is guaranteed to be safe and under your control, and if conciseness is a priority, using eval() might be a viable option. However, if security, maintainability, and readability are of greater concern, manually parsing and processing the file content using appropriate functions is generally recommended.

Remember to carefully evaluate the risks and consider the specific needs of your project before deciding on the approach to use.

1 Like

Note that using strip and alike is more common to other languages, meaning it is something more useful to learn and use anyhow. It also forces to understand what is happening instead of relying on some sort of automatic magic.

1 Like

This is not an exact code problem. But just me not understanding the exact process, that’s going on with the open/ close things and want to understand better.

f.open("pizza.orders","r")
pizzaorders = eval(f.read())
f.close() 

why do we want to open and close the file from the start?

I mean what difference does it make?

Is it still somewhere in the ram? Since I thought close was the “save” command.

There is an active file pointer which should always be closed when possible because it takes up system resources.
This method is preferred because it makes it clear where the file pointer is being used, and means you don’t have to .close the file:

# Output contents of file
with open("pizza.orders") as orders:
    print(orders.read())

Note that if you’re opening multiple files at once, you could do it as

with open("file1.txt") as f1, open("file2.txt") as f2:
    ...
4 Likes

One question for UMAR. Will the with function work if there is no existing text file? If not, will a try except block have to be used?

To Panda: In the case you posted, the text file is being converted into a list, stored as a list named pizzaorders, and then closed. So the information is being duplicated into memory, named pizzaorders

3 Likes

Thanks both of you :slight_smile:

I’m having a hard time understanding the logic behind the eval function.
Please help a brother out.

Hi @ApoorvGoyal2 the eval function executes the instruction inside the bracket but only if it is a valid function.

Please see Python eval() Function for more details and examples.

2 Likes

Hello! I don’t know what to do because the challenge area was not descriptive. What do we have to do?

You need to make a to-do list manager with 4 features: adding, viewing, removing, and editing. Each item of the to-do list is, well, a list, with a name, when it needs to be done, and how important it is (high, medium, low).

1 Like

@QwertyQwerty88 I did all of that on Day 45… The challenge on Day 51 says "Remember the early days when all this was just lists?

Good! Get back over to Day 45 and grab your to do list code. You’ll need it today.

Improve your to do list to add auto-save and auto-load.

That’s it. Go get 'em, tiger!

"

Oh whoops. But you said it yourself:

2 Likes

@QwertyQwerty88 I don’t know what that means…

Well it’s what they teach in this lesson.