#๐Ÿ”’ Please Explain why a new key value pair is not added in .json there

69 messages ยท Page 1 of 1 (latest)

unique hamlet
stray siloBOT
#

@unique hamlet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

agile bramble
#

whats g? take it out

#

btw if you want the json readable, (tasks, indent=4)

rough granite
# unique hamlet

it's complaining on line 39, but your screenshot doesn't show that many lines

unique hamlet
gritty tusk
#

I don't think it's writing to file now

gritty tusk
#

I still sincerely suggest you not to use argparse as it's not the right tool

unique hamlet
gritty tusk
#

And you also haven't seen to listen to the recommendations of using a subparser from other user

gritty tusk
#

Like, it might work, and it's not how the tool should use and just confusing people

stone jay
unique hamlet
stone jay
#

Can you make a smaller program (with no inputs/files) demonstrating what : does?

#

I don't know what you think it does, but it's not that

#

(it's a pretty obscure feature that you shouldn't worry about for now)

#

!e
(It's a "variable annotation") ```py
x: int

print(annotations)

stray siloBOT
stone jay
#

I don't understand

#

I'm referring to this line of code

#

you can assign an item to a dictionary by key using this syntax:

tasks[the_key] = the_value
#

=, not :

unique hamlet
#

oh ic

unique hamlet
stone jay
# unique hamlet

That means that tasks is a list, but you're trying to treat it like a dictionary

unique hamlet
stone jay
#

Do you know how to make an empty dictionary?

unique hamlet
stone jay
#

Doesn't [...] create a list?

unique hamlet
stone jay
unique hamlet
covert glade
#

Surely you need to fix the {get_id} too in that f-string.

unique hamlet
stone jay
unique hamlet
stone jay
#

Iterating over a dictionary will give you its keys, not its values, as this small program demonstrates

#

The next chapter on functions will also be helpful

rough granite
#

using a dictionary here is just making things harder for you, using a list would have made things much easier

unique hamlet
unique hamlet
stone jay
stone jay
unique hamlet
stray siloBOT
rough granite
stone jay
unique hamlet
#

dictionary = { "key": "value"}

stone jay
#

right

#

you're iterating over the keys

#

!e
each key is a string, and you're trying to index it: ```py
task = "task 123"
print(task["id"])

stray siloBOT
unique hamlet
#

oh ok then what should be done?

stone jay
stone jay
rough granite
#

the task id is a number, that increases when a new task is added
when updating a task the task is identified by the id, also very easy to just index into the list and update the value of that element
deleting a task, even easier with a list, as long as it's acceptable that the id numbers of all following tasks decrement by one too
again, perfect use cases for a list

unique hamlet
#

yeah lol i will try with both

stone jay
#

I think both can work

#

If you're more familiar with lists, do it with lists first

unique hamlet
rough granite
#

i see that your code is trying to find the highest id/key in the dictionary by looping through it
using a list you don't even have too, as you would just .append() to the list
but if you are going to do that with a dictionary you could as well just do something like max(tasks) instead of that loop

unique hamlet
#

idk bro how much time will it take to perfect it like all of u ๐Ÿ˜ญ

rough granite
stray siloBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.