lst = [[5, 12, 17, 21, 17], [20, 14, 11,7, 17]]
def get_numbers(lst):
numdic = {}
for l in lst:
for n in l:
if numdic[str(n)]==None:
numdic[str(n)] = 1
else:
numdic[str(n)] += 1
return numdic
print(get_numbers(lst).get("15",0))```
my intention is to calculate how many each unique numbers are in the list of list.
and printing out how many 15 is in the lst.
#๐ what is KeyError?
129 messages ยท Page 1 of 1 (latest)
@lean pivot
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.
Closes after a period of inactivity, or when you send !close.
You get KeyError if you try to access a dict entry which isn't there. Your numdict starts empty and the first thing you do is access an entry.
Probably this test: numdic[str(n)]==None
should be:
str(n) not in numdict- or
numdict.get(str(n)) is None
BTW, why are you using str(n) fo the dict key instead of just n ? @lean pivot
Yes, that
numdic["key"]
But why? dict keys do not need to be strings.
Well you can use ints directly.
Can this be {1:1, 2:32} ??
Yes
Oh I didn't do the initialising
Should've use "is" for None
Your KeyError came from this: numdic[str(n)]==None
That first evaluates numdic[str(n)], which asks for the entry with key str(n). That raises key error if the key is not there.
๐
numdict.get(str(n)) returns the value, or None if it isn't there.
Like I said, numdic[str(n)] tries to access that entry, and it's not there.
You want numdic.get(str(n)) is None
None means not there ??
None is used in Python for "no meaningful value".
The return value from dict.get is None if the key is not there.
The return value from numdict[key] is always the value, and it raises KeyError if the key is not there.
If you're probing for the key you can either use .get(key) and test the result for is None (key not there) or you can ask key not in numdict, which will be true if the key is not there
Ahhh only that line. I guess it won't be that much of bad marks for that ?????
I thought None was the value
Well it does break the whole script. But it's a small error.
Null value
No. None is what is returned from dict.get if there is no value in the dict
It was one of question in exam which doesn't allow me to use the interpreter
So I just used text page
There are languages where they pretend the entry is there and serve up None (or null or whatever their not-a-value value is). But in Python, accessing a key which is not there gets a KeyError
Then your mistake won't cost you too much.
noice
Well, I assume so anyway.
If you don't get to run and debug your script, the marking has to be more forgiving.
If I get more than 45 out of 60 . I will get the highest mark
It was like 6 of them were coding. The rest 32 was short answer
See your last line went numdict.get(15,0)? That's written to cope with no 15s at all.
I checked I already lost 1 point in py what is x[3:-2]? , x = [4,6,8,10,12] I answered it is py type list and value [10]
Yes
What did you answer for this?
It would be []
Yeah type is list which gets me +1., but not the value.
Your start/stop index are the same index, which returns nothing
it would be the same as trying to slice [3:3]
The behind slice exclude 10
Why would somebody type [3:3]
It's more to make sure you can figure out what it means.
Definitely lost 1 point there
So hope I didn't get more wrong than 14 points
But I did a bit complicated coding (rather too personal or obnoxious idk how to explain it ) on coding problems
Not the clean, "looks the right way" coding
One more question
What would you say .
What this will exactly print?
print("cherry")```
what did you put?
Yeah but
print prints the value of the string, which is the text inside the quotes
On the blank thing
When I have to write the type and value
If the type is str
Value should be in " "
So I did "cherry"
yes, that's how you create new strings
The quote marks just mark out, to Python, where the string starts and ends
but that's not how they display
the value of the string itself is the text between
If the value is without the double quotes I'm screwed
Because in my memory
With type, value questions
I always had to do double quotes for str value
If you go x="aa"
Let me check again
then the value of the text is aa
but you need the quotes in the python code to mark that value out as a string
print(x) would just print aa
and a newline
Well yes
it entirely depends what you're asking
"haha" is a string literal
Let me check how I got in the example exam
Yeah they wanted double quotes for value
๐ฎโ๐จ
Thank my memory
following expression
So "cherry" would be wrong I guess ?
's' and "s" should both be right
it depends what's being asked
That's -2 or -3 total for now excluding coding mistakes
the value is not always equal to what is printed
if they marked 's' wrong, I would seriously bring it up with your teacher
if anything, 's' is more correct because python uses ' interally for its literals
No I mean both "s" 's' is right
!e
print(repr("s"))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
's'
Print("cherry")
then the output would be cherry without quotes

Already -2 or -3 points
Well I've been lazy and was just worrying about the exam not keeping up with it since I was getting another exam because my laptop didn't work out on the normal exam date
I just used last 3 days for preparation which was

But it felt okay......
Thank you for the answer! I'll close this
!close
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.