mercredi 15 avril 2020

Replacing if-else statements efficiently in Python 3.x [duplicate]

I have a series of if else statements like this and I want to change it to switch case statement (or something else which is efficient and recommended in Python 3.x):

if x >= 110:
    print("110 precent or more")
elif x >= 100:
    print("100 precent or more")
elif x >= 90:
    print("90 precent or more")
elif x >= 50:
    print("50 precent or more")
else:
    print("Ignore, yet to reach 50 percent")

I tried this but it seems Python does not support a condition in case statements:

switch (x) {
        case x >= 110:  print("110 precent or more");
                    break;
        .......
        .......
        .......
        default: print("Ignore, yet to reach 50 percent");
                    break;
    }

Aucun commentaire:

Enregistrer un commentaire