mardi 8 décembre 2020

How can I make an exception handling inside a for loop?


def show():
    
    for x in d.items():
        ans = fieldinR1.get()
   #   print(d.get(ans))
        fieldout.insert(0,(d.get(ans)))
        break
        if ans not in d:
            fieldout.insert(0,"Not a valid answer! Please try again ...")

This function is used for retrieving values from a dictionary ("d") through entering the keys in a tk entry (fieldinR1). The answer is presented in another entry (fieldout)). The functionality works fine if the keys are in the dictionary, but I want an exception handling if the input is not in the dict. What is a good way to write an exception handling here? I am currently trying with the "if ans not in d:"

The error message looks like this:

_tkinter.TclError: wrong # args: should be ".!entry2 insert index text"
```

I get the error message when the exception handling is called but there are no complaints when I input something thet is in the dictionary.

Aucun commentaire:

Enregistrer un commentaire