Python Forum
call dict object result key error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
call dict object result key error
#1
hello, my program include a dict, but when I try to call a object of the dict, that result with a KeyEror
actualsearch=0
dict={ 0: 41, 1: 41, 2: 41, }

if dict[int(actualsearch)]==41: #I try int and str, but both dosen't work
    do something
    actualsearch=actualsearch+1
I want to call the dict object whit the var actualsearch, but they call this error
Error:
if dict[int(actualsearch)]==41: KeyError: '0'
if you can help my, I will be thankful
Reply
#2
Actually, your code works as it is now. The KeyError '0' , i.e. - the key is str shown suggest you tried with
if dict[str(actualsearch)]==41:
actualsearch=0
dict={0:41, 1:41, 2:41}
 
if dict[int(actualsearch)]==41: #I try int and str, but both dosen't work
    actualsearch=actualsearch+1
    print(actualsearch)
output
Output:
1
several other things

1. don't use dict as variable name, it's a built-in function and you override it
2. actualsearch is int, the keys in the dict are also int. there is no need of any conversion
3. it's good to use dict.get() method to avoid KeyError if key is missing
actual_search = 0
my_dict = {0:41, 1:41, 2:41}
 
if my_dict.get(actual_search) == 41:
    # do something
    actual_search += 1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
the dict name was for the example, is not the real name, and thank for the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 5 680 Jan-10-2024, 10:55 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 777 Jul-23-2023, 02:25 PM
Last Post: Fare
  Error in Int object is not subscript-able. kakut 2 1,131 Jul-06-2022, 08:31 AM
Last Post: ibreeden
  'int' object is not subscriptable after API call ed8484 1 1,765 Sep-18-2021, 02:06 PM
Last Post: ed8484
Star Type Error: 'in' object is not callable nman52 3 3,328 May-01-2021, 11:03 PM
Last Post: nman52
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,386 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  how to call an object in another function in Maya bstout 0 2,042 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,417 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 2,195 Aug-03-2020, 02:28 PM
Last Post: Larz60+
  Python: Call function with variabele? Ending in error. efclem 5 2,880 Apr-22-2020, 02:35 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020