79116350

Date: 2024-10-23 05:22:37
Score: 0.5
Natty:
Report link

I am not sure about the exact issue as the code you provided is not enough to identify it, however the error you shared

Traceback (most recent call last):
  File "C:\Users\hajar\Desktop\lns_v_test_epsilon\lns\test_alpha_gamma.py", line 128, in <module>
    main()
  File "C:\Users\hajar\Desktop\lns_v_test_epsilon\lns\test_alpha_gamma.py", line 124, in main
TypeError: 'dict_keys' object is not subscriptable

Clearly points that you are trying to access a dict_keys item by indexing it, which is not possible. If you want to access a key using its index, you need to first convert the dict_keys object into something that can be indexed, such as a list or a tupple:

foo = {'bar':0}

try:
   key = foo.keys()[0]
except:
   print('It doesnt work...')
   key = list(foo.keys())[0]
   print('... but now it does! ->', key)

Note that foo.keys() returns a class dict_keys which can be iterated (but not indexed) through to get both the index and key itself:

foo = {'bar0':0, 'bar1':10}

for ii, key in enumerate(foo.keys()):
   print('Key index: ', ii)
   print('Key name: ', key)

Reasons:
  • Blacklisted phrase (1): doesnt work
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: greg salles