Bottom Page

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

if dict[int(actualsearch)]==41: #I try int and str, but both dosen't work
    do something
I want to call the dict object whit the var actualsearch, but they call this error
if dict[int(actualsearch)]==41: KeyError: '0'
if you can help my, I will be thankful
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:
dict={0:41, 1:41, 2:41}
if dict[int(actualsearch)]==41: #I try int and str, but both dosen't work
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
the dict name was for the example, is not the real name, and thank for the help!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Sympy error "object is not callable" Cupcake 0 93 Feb-08-2020, 02:22 AM
Last Post: Cupcake
  How to mock an object that is created during function call? Schlangenversteher 0 128 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  object is not subscriptable... Error Message? Shafla 3 810 Sep-25-2019, 12:27 AM
Last Post: buran
  Error: int object does not support item assignment ankita_nthu 2 1,591 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  API Call Gets 404 Error jimbone30 0 284 Jun-28-2019, 04:06 AM
Last Post: jimbone30
  call func from dict mcmxl22 3 481 Jun-21-2019, 05:20 AM
Last Post: snippsat
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 526 Jun-11-2019, 04:14 PM
Last Post: pcarra
  Error 'object has no attribute' when iterating thru a dictionary mrapple2020 8 1,512 Apr-08-2019, 01:36 AM
Last Post: mrapple2020
  Getting error "Type error-a bytes-like object..." mrapple2020 1 2,035 Apr-06-2019, 06:37 PM
Last Post: mrapple2020
  Why doesn't gc delete an object without forcing a garbage collection call? AlekseyPython 5 722 Mar-19-2019, 02:10 AM
Last Post: micseydel

Forum Jump:

Users browsing this thread: 1 Guest(s)