1

How can I decrease lines of code when I enter a number in text format and I need to assign a code consecutively to that number?

Attached is an example:

def plataformaVSTP(id):
    codigoFalla = "N/A"
    if id == "70000":
        codigoFalla = "VSTP020"
    elif id == "70002":
        codigoFalla = "VSTP021"
    elif id == "70005":
        codigoFalla = "VSTP022"
    elif id == "70007":
        codigoFalla = "VSTP023"
    elif id == "70008":
        codigoFalla = "VSTP024"
    elif id == "70010":
        codigoFalla = "VSTP025"
    elif id == "70011":
        codigoFalla = "VSTP026"
    elif id == "70015":
        codigoFalla = "VSTP027"
    elif id == "70020":
        codigoFalla = "VSTP028"
    elif id == "70031":
        codigoFalla = "VSTP029"
    elif id == "70050":
        codigoFalla = "VSTP030"
    elif id == "70457":
        codigoFalla = "VSTP031"
    elif id == "70505":
        codigoFalla = "VSTP032"

and so on...

I currently have more than 450 elif, of how the code is displayed.

I don't know if the above slows down the execution of my script.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 6
    Consider using a dictionary. Notice the syntax highlighting on `id`. It's a builtin function. A common convention is to write `id_` in such cases. – Passerby Jan 25 '22 at 01:34
  • Is there a rule which id's exist? If there is no rule, you have no other choice than to type them all out. – mkrieger1 Jan 25 '22 at 01:37
  • @mkrieger1 are all these IDs, and I must assign them a code with a correlative number from VSTP20 onwards. 7000 to 70505, unfortunately, it does not have a logic of the jumps, even, odd, etc. – Cristian cortes rojas Jan 25 '22 at 01:44
  • Does this answer your question? [Replacements for switch statement in Python?](/q/60208/4518341) I second the dict suggestion. – wjandrea Jan 25 '22 at 02:21

2 Answers2

1

The use of a dictionary would great improve the writeability and readability of the code. This you would implement such dictionary.

dictionary = {"70000":"VSTP020","70002":"VSTP021"..... and so on}

the check what value the variable codigoFalla

should get use this code:

codigoFalla = dictionary["70000"] # sets the value of codigoFalla to the value corresponding to the key "70000"
  • thank you, I thought of doing it this way: def plataformaVSTPDictionary(id): def plataformaVSTPDictionary(id): dictionary = {"70000":"VSTP020","70001":"VSTP021"} try: return dictionary[id] except: return "N/A" – Cristian cortes rojas Jan 25 '22 at 02:43
1
def plataformaVSTP(id_):
    default = "N/A"
    codigoFalla = {
        "70000": "VSTP020",
        "70002": "VSTP021",
        "70005": "VSTP022",
        "70007": "VSTP023",
        "70008": "VSTP024",
        "70010": "VSTP025",
        "70011": "VSTP026",
        "70015": "VSTP027",
        "70020": "VSTP028",
        "70031": "VSTP029",
        "70050": "VSTP030",
        "70457": "VSTP031",
        "70505": "VSTP032",
    }
    return codigoFalla.get(id_, default)
Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79
  • thanks for the help, I had thought of it this way, but your way makes more sense. Thank you very much for your help. `def plataformaVSTPDictionary(id): dictionary = {"70000":"VSTP020","70001":"VSTP021"} try: return dictionary[id] except: return "N/A"` – Cristian cortes rojas Jan 25 '22 at 02:51