dimanche 4 avril 2021

Is it possible to shorten my if elif statements to assign a variable value within each if condition? (Python)

Here's the code I am using.

if i < 50:
    test_a = os.getenv("TEST_VAR_A")
elif i >= 50 and i < 100:
    test_a = os.getenv("TEST_VAR_B")
elif i >= 100 and i < 150:
    test_a = os.getenv("TEST_VAR_C")
elif i >= 150 and i < 200:
    test_a = os.getenv("TEST_VAR_D")
elif i >= 200 and i < 250:
    test_a = os.getenv("TEST_VAR_E")
elif i >= 250 and i < 300:
    test_a = os.getenv("TEST_VAR_F")
elif i >= 300 and i < 350:
    test_a = os.getenv("TEST_VAR_G")
elif i >= 350 and i < 400:
    test_a = os.getenv("TEST_VAR_H")
else:
    test_a = "test"

Is it possible to shorten it? Say in one line?

Thanks!

Aucun commentaire:

Enregistrer un commentaire