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
#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


Messages In This Thread
RE: call dict object result key error - by buran - May-14-2019, 05:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,088 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  Need help with 'str' object is not callable error. Fare 4 861 Jul-23-2023, 02:25 PM
Last Post: Fare
  Error in Int object is not subscript-able. kakut 2 1,191 Jul-06-2022, 08:31 AM
Last Post: ibreeden
  'int' object is not subscriptable after API call ed8484 1 1,824 Sep-18-2021, 02:06 PM
Last Post: ed8484
Star Type Error: 'in' object is not callable nman52 3 3,419 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,465 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  how to call an object in another function in Maya bstout 0 2,089 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,508 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 2,239 Aug-03-2020, 02:28 PM
Last Post: Larz60+
  Python: Call function with variabele? Ending in error. efclem 5 2,970 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