Python 3:如何让else语句仅在所有if语句均不成立时执行?
Hey there! I get exactly what you're trying to do—you want that else clause to only fire when none of your target letter combinations are found in the input sequence. The issue with using separate if statements followed by an else is that the else only pairs with the very last if, not all of them together. Let's fix that with two straightforward approaches.
Approach 1: Use a "Found" Flag Variable
This is the most flexible method, especially if you want to detect multiple matching sequences (like if both "abc" and "def" are present in the input). Here's how to adjust your code:
class color: BOLD = '\033[1m' END = '\033[0m' GREEN = '\033[92m' print("Welcome to the Sequence Scanner") print(" ") # Adding a space between welcome message and input seq = input("Provide your sequence: ") # Initialize a flag to track if any sequence was found found_match = False # Check each target sequence if "abc" in seq: print(color.GREEN + color.BOLD + "Found sequence 'abc'!" + color.END) found_match = True if "def" in seq: print(color.GREEN + color.BOLD + "Found sequence 'def'!" + color.END) found_match = True if "ghi" in seq: print(color.GREEN + color.BOLD + "Found sequence 'ghi'!" + color.END) found_match = True # Only run this if NO matches were found if not found_match: print("No target sequences detected in your input.")
How this works:
- We start with
found_matchset toFalse. - Every time we find a matching sequence, we flip the flag to
True. - After checking all targets, we check if the flag is still
False—if so, that means none of the sequences were found, so we print the "no matches" message.
Approach 2: Use an If-Elif-Else Chain (For Mutually Exclusive Matches)
If you only care about finding at least one match (and don't need to detect multiple overlapping matches), you can chain your checks with elif and end with a single else:
class color: BOLD = '\033[1m' END = '\033[0m' GREEN = '\033[92m' print("Welcome to the Sequence Scanner") print(" ") seq = input("Provide your sequence: ") if "abc" in seq: print(color.GREEN + color.BOLD + "Found sequence 'abc'!" + color.END) elif "def" in seq: print(color.GREEN + color.BOLD + "Found sequence 'def'!" + color.END) elif "ghi" in seq: print(color.GREEN + color.BOLD + "Found sequence 'ghi'!" + color.END) else: print("No target sequences detected in your input.")
Note: This method stops checking once it finds the first match. So if your input has both "abc" and "def", it will only report "abc" and skip the rest. Use this only if you don't need to detect multiple matches.
Which One Should You Choose?
- Use the flag method if you want to find all matching sequences (e.g., highlight every target combination present in the input).
- Use the elif chain if you only need to know if at least one sequence exists (and don't care about multiple matches).
Either way, both approaches will ensure your "no matches" message only appears when none of your target sequences are found.
内容的提问来源于stack exchange,提问作者braxsch

