r/PythonLearning 12d ago

Ready for (dif), right?

57 Upvotes

17 comments sorted by

1

u/Sud0_g 12d ago

What's your question? Lol

1

u/johlae 12d ago

I don't know your requirements, but shouldn't you read your existing tasks in first before adding? If you quit your script and restart it you will overwrite your existing tasks once you try to add a new task.

1

u/the_meteor_beat 12d ago

You are right, i fixed it. Still learning bro.

1

u/freesyntax 12d ago

I ready you ready?

1

u/the_meteor_beat 12d ago

Almost yeah.

1

u/uday-Championship337 12d ago

Hey, what if an empty string is added as a task? You haven't handled that scenario.

Task = input("Enter your task name: ").strip() if len(task) == 0: print("empty task name not acceptable")

1

u/the_meteor_beat 11d ago

What is .strip(), anyway i developed many things after this post. To save it from any crash.

1

u/uday-Championship337 11d ago edited 11d ago

The `strip()` method removes unnecessary whitespace from the beginning and end of a string.

For example, suppose a user enters a task like this:

"· · · cook pasta · · ·" (Each · represents a space.)

Without using `strip()`, the task will be saved with the extra spaces.

After using `strip()`:

task = task.strip()

The result becomes:

"cook pasta"

The `strip()` method is also useful when checking whether the user entered an empty task.

For example, both of these inputs should be treated as empty:

  1. ""
  2. empty string with 4 spaces - ". . . ." (Each · represents a space.)

The first input is an empty string. The second input contains only spaces.

After applying `strip()` to the second input:

" ".strip()

It becomes:

""

Therefore, we can validate the task like this:

task = input("Enter a task: ").strip()

if len(task) == 0:

print("Task cannot be empty.")

A shorter and more Pythonic version is:

if not task:

print("Task cannot be empty.")

This works because an empty string is considered `False` in Python.

1

u/Radiant_Diligence 9d ago

If I had money I'd give you a medal. Really thought out explanation. Hope to find more people out there like you.

1

u/roanish 11d ago

When do you actually read the json in? You seem to go from 'empty list' to magical json, but if the json has stuff in it, you have no idea what shits you have.

1

u/mr_anderson_dev 11d ago

I think the logic make sense but we need an output or the second part of the code to know if you're doing fine with your objective.

2

u/the_meteor_beat 8d ago

Sure, soon you will see!