Home > front end >  how to elimate the need for a large number of if elif else statements
how to elimate the need for a large number of if elif else statements

Time:09-09

My script uses a if elif else check to determine which array item to look at for crafting values. The issue is there are a total of 24 elif statements to allow for a total of 26 choices, (1 for the if, 1 for error catching). What I'm trying to figure out is how to reduce the number of elif statements so the code is better structured.

while choice == 0:
    choice = input("Choose a block (Enter only the Number):")
    if not choice.isalpha(): # Makes sure that the input is a number and not a string.
        choice = int(choice)
    else:
        choice = 0
        print("Thats not a number. Choose a number Numbnuts.")
    if choice == 1:
        print("\n",block[0])
    elif choice == 2:
        print("\n",block[1])
    elif choice == 3:
        print("\n",block[2])
    elif choice == 4:
        print("\n",block[6])
    elif choice == 5:
        print("\n",block[7])
    elif choice == 6:
        print("\n",block[8])
    elif choice == 7:
        print("\n",block[3])
    elif choice == 8:
        print("\n",block[4])
    elif choice == 9:
        print("\n",block[5])
    elif choice == 10:
        print("\n",block[9])
    elif choice == 11:
        print("\n", block[10])
    elif choice == 12:
        print("\n", block[11])
    elif choice == 13:
        print("\n",block[12])
    elif choice == 14:
        print("\n",block[13])
    elif choice == 15:
        print("\n",block[14])
    elif choice == 16:
        print("\n",block[15])
    elif choice == 17:
        print("\n",block[16])
    elif choice == 18:
        print("\n",block[17])
    elif choice == 19:
        print("\n",block[18])
    elif choice == 20:
        print("\n",block[19])
    elif choice == 21:
        print("\n",block[20])
    elif choice == 22:
        print("\n", block[21])
    elif choice == 23:
        print("\n", block[22])
    elif choice == 24:
        print("\n",block[23])
    elif choice == 25:
        print("\n",block[24])
    elif choice == 26:
        print("\n",block[25])

CodePudding user response:

Look for structure:

elif choice == 12:
    print("\n", block[11])
elif choice == 13:
    print("\n",block[12])
elif choice == 14:
    print("\n",block[13])
elif choice == 15:
    print("\n",block[14])
elif choice == 16:
    print("\n",block[15])

Clearly, this is the same as;

print("\n", block[choice - 1])

However, for choice in {4,5,6,7,8,9} the logic isn't that simple, so you could keep the ifs:

if choice == 4:
        print("\n",block[6])
elif choice == 5:
        print("\n",block[7])
elif choice == 6:
        print("\n",block[8])
elif choice == 7:
        print("\n",block[3])
elif choice == 8:
        print("\n",block[4])
elif choice == 9:
        print("\n",block[5])
else:
        print("\n", block[choice - 1])

You could also go a step deeper: for choice in {4,5,6} the index is choice-2, and for choice in {7,8,9} the index is choice-4:

if choice in {4,5,6}:
   idx = choice - 2
elif choice in {7,8,9}:
   idx = choice - 4
else:
   idx = choice - 1

print("\n", block[idx])

CodePudding user response:

As another answer suggested, look for structure; you have different ranges of choices that correspond to different offsets in block, so you can iterate over those:

while True:
    try:
        choice = input("Choose a block (Enter only the Number):")
    except ValueError:
        print("Thats not a number. Choose a number Numbnuts.")
        continue
    if choice < 1 or choice > 26:
        continue
    print("\n")
    for limit, offset in ((3, -1), (6, 2), (9, -4), (26, -1)):
        if choice <= limit:
            print(block[choice   offset])
            break
    break

Hence for 1 <= choice <= 3 you print block[choice-1], for 4 <= choice <= 6 you print block[choice 2], and so on.

  • Related