#๐ Please Explain why a new key value pair is not added in .json there
69 messages ยท Page 1 of 1 (latest)
@unique hamlet
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.
it's complaining on line 39, but your screenshot doesn't show that many lines
no that was earlier after that its not showing that error
I don't think it's writing to file now
oh, i see
I still sincerely suggest you not to use argparse as it's not the right tool
why? but
And you also haven't seen to listen to the recommendations of using a subparser from other user
Where the add argument?
Like, it might work, and it's not how the tool should use and just confusing people
On line 22 you probably meant to use = instead of :
no it throws me error also it just checks if aruguement is passed or not with :
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{'x': <class 'int'>}
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 :
oh ic
That means that tasks is a list, but you're trying to treat it like a dictionary
so how to make it dictionary
Do you know how to make an empty dictionary?
c = [{}]
Doesn't [...] create a list?
still
What's in the tasks.json file?
it worked bro thanks uuu
Surely you need to fix the {get_id} too in that f-string.
when i tried adding second line this showed
What will this program print?
fruit_prices = {"apple": 10, "banana": 20}
for fruit in fruit_prices:
print(fruit, type(fruit))
but mine one is returning theid+1
Iterating over a dictionary will give you its keys, not its values, as this small program demonstrates
I would recommend reading https://beej.us/guide/bgpython/html/split/dictionaries.html#iterating-over-dictionaries (and maybe the entire page on dictionaries)
The next chapter on functions will also be helpful
using a dictionary here is just making things harder for you, using a list would have made things much easier
but i m using value and not key still
but i m learning rn so want to make thing from basics
What do you mean by "using the value"?
!e
fruit_prices = {"apple": 10, "banana": 20}
for fruit in fruit_prices:
print(fruit, type(fruit))
i mean i m putting get_id(tasks) and not get_id
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | apple <class 'str'>
002 | banana <class 'str'>
lists are basic, i would say more basic then dictionaries
and would probably be more fitting here
Do you know what "keys" and "values" mean in a dictionary?
dictionary = { "key": "value"}
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"])
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 2, in <module>
003 | print(task["id"])
004 | ~~~~^^^^^^
005 | TypeError: string indices must be integers, not 'str'
oh ok then what should be done?
If you want to iterate over the values, you need to use the values() method of the dictionary. If you want to iterate over both keys and values, you need the items() method
this page has some examples and explanation
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
yeah lol i will try with both
now its not showing error but not storing as well;
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
idk bro how much time will it take to perfect it like all of u ๐ญ
don't try to make it perfect from the beginning
first just make something that work
then i you can iteratively improve it to the point that you are content with the solution for the time being, you can always revisit it later if you would like too improve it even further in the future
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.