0

I am writing a script to manipulate an input file in different ways. The user will specify which method to use: OPTION_1, OPTION_2, or OPTION_3. There is also an option to perform all of these methods sequentially, ALL.

I am having trouble getting the script to continue executing when the ALL option is selected. Here is what I have at the moment:

calculation_type = "ALL"  # ALL, OPTION_1, OPTION_2, OPTION_3

if calculation_type == "ALL":
        print("Calculation type " + calculation_type + " chosen.")

elif calculation_type in {"ALL","OPTION_1"}:
    if calculation_type != "ALL":
        print("Calculation type " + calculation_type + " chosen.")
    # Manipulate file

elif calculation_type in {"ALL","OPTION_2"}:
    if calculation_type != "ALL":
        print("Calculation type " + calculation_type + " chosen.")
    # Manipulate file

elif calculation_type in {"ALL","OPTION_3"}:
    if calculation_type != "ALL":
        print("Calculation type " + calculation_type + " chosen.")
    # Manipulate file


print("Calculation finished") 

This works fine for OPTION_1/2/3, but when the choice is ALL the script gets stuck at the first if statement and does not continue onto the second if statement. How can I make the script execute each if statement sequentially when ALL is selected?

5
  • 4
    replace elif with if. Commented Feb 18, 2021 at 13:57
  • 4
    Why do you have to check if ALL is in the set anyways? If calculation_type = "ALL", the first statement executes. You then have a nested if inside each elif that will only execute its body if the calculation_type is not "ALL". So, what's the point of checking if calculation_type might be "ALL" in each elif statement? Commented Feb 18, 2021 at 13:57
  • Maybe you want calculation_type to be a set as well, if it could contain, say, OPTION_1 and OPTION_2 but not OPTION_3. Commented Feb 18, 2021 at 14:12
  • @chepner, there's no eval call. Commented Feb 18, 2021 at 14:37
  • @gmdev, I haven't included the rest of the code which will sit in each if statement for the sake of brevity, but there is more that needs to be done in each loop when the type is ALL/OPTION_1, ALL/OPTION_2 etc. So there is more than just the printing to be done Commented Feb 18, 2021 at 15:47

2 Answers 2

1

Without involving sets, you want a straightforward set of three separate if statements. Each explicitly checks for ALL or a specific option.

if calculation_type == "OPTION_1" or calculation_type == "ALL":
    ...
if calculation_type == "OPTION_2" or calculation_type == "ALL":
    ...
if calculation_type == "OPTION_3" or calculation_type == "ALL":
    ...

Using sets, the structure is the same:

if calculation_type in {"OPTION_1", "ALL"}:
    ...
if calculation_type in {"OPTION_2", "ALL"}:
    ...
if calculation_type in {"OPTION_3", "ALL"}:
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Great, this is what I wanted! I originally had all the statements as ifs but then changed them to elifs to troubleshoot a different problem that has since been solved, and forgot to change them back. Thanks!
1

Remove the elif statements and use if. elif is only executed if the previous if fails, which is not the case when the type is ALL.

The following would be more correct:

calculation_type = "ALL"  # ALL, OPTION_1, OPTION_2, OPTION_3

if calculation_type == "ALL":
        print("Calculation type " + calculation_type + " chosen.")

if calculation_type == "OPTION_1":
        print("Calculation type " + calculation_type + " chosen.")
    # Manipulate file

if calculation_type == "OPTION_2":
        print("Calculation type " + calculation_type + " chosen.")
    # Manipulate file

if calculation_type == "OPTION_3":
        print("Calculation type " + calculation_type + " chosen.")
    # Manipulate file


print("Calculation finished")

1 Comment

This doesn’t seem to be what the OP requires.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.