samedi 11 avril 2020

is using elif for exact value in python recommended?

def scan(snippet):
    Database = {"direction": ["north", "south", "east", "south"],
        "verb" : ["go", "kill", "eat"], "stop": ["the", "in", "of"],
        "noun" : ["bear", "princess"]}

    snippets = snippet.split()
    data = []

    for i in snippets:
        try:
            data.append(("number", str(int(i))))                

        except ValueError:
            for j in Database.keys():
                if i in Database[j]:
                    data.append((j, i))
                    break

                elif j == "noun":
                    data.append(("error", i))

    return data

So i was wondering if elif j=="noun" is a good practice, is there any other way? This is a code for recognizing advance user input found in book "learn python 3 the hard way"

Aucun commentaire:

Enregistrer un commentaire