I am writing a script that, if an array's elements are the subset of the main array, then it print PASS, otherwise, it print FAIL instead.
What should I add to my if-else statement below to make it works?
a = [1,2,3,4,5]
b = [1,2]
c = [1,9]
# The Passing Scenario
if (i in a for i in b):
print "PASS"
else:
print "FAIL"
# The Failing Scenario
if (i in a for i in c):
print "PASS"
else:
print "FAIL"
The elif clause in Python In case else is not specified, and all the statements are false , none of the blocks would be executed. Here's an example: if 51<5: print("False, statement skipped") elif 0<5: print("true, block executed") elif 0<3: print("true, but block will not execute") else: print("If all fails.")
The if/else if statement allows you to create a chain of if statements. The if statements are evaluated in order until one of the if expressions is true or the end of the if/else if chain is reached. If the end of the if/else if chain is reached without a true expression, no code blocks are executed.
Definition and Usage. The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions.
Shorthand If and If Else in Python Shorthand if and if else is nothing but a way to write the if statements in one line when we have only one statement to execute in the if block and the else block.
Use all
.
# the passing scenario
if all(i in a for i in b):
print 'PASS'
else:
print 'FAIL'
# the failing scenario
if all(i in a for i in c):
print 'PASS'
else:
print 'FAIL'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With