So I’m trying to get this working, where I remove the week’s stats (weeklydict) from this second’s stats (instantdict) so I have an accurate weekly progress for all keys of instantdict (keys being members). It works fine and dandy, but when a new member joins (adding to the keys in instantdict), shit hits the fan, so I use try/except, and attempt to add the missing member to weeklydict too, except when I do that using except keyerror as e and str(e), I’m given a ‘none’ value. Any idea on what to do?
Code:
for member, wins in instantDict.items(): try: instantDict[member] = instantDict[member] - weeklyDict[member] except KeyError as e: weeklyDict[str(e)] = instantDict.get(str(e)) #error occurs here instantDict[member] = instantDict[member] - weeklyDict[member] #thus fucking this up
Advertisement
Answer
Based on my testing, str(e)
returns a string as such:
"'test'"
The value is a string displaying a string, so .get()
is not finding the value. Try something like:
for member, wins in instantDict.items(): try: instantDict[member] = instantDict[member] - weeklyDict[member] except KeyError as e: weeklyDict[str(e).strip("'")] = instantDict.get(str(e).strip("'")) instantDict[member] = instantDict[member] - weeklyDict[member]
That should take the extra string characters off of the keyword, and allow .get()
to actually find the value.
Alternatively, if you know that it errored because you know that member
is not in the dictionary, why pull the exact same variable from the exception when you could just use member
again?