Chapter 11
Else and Elif
Statements
If proclamations you've coded so far have been
win or bust. On the chance that the condition tried validly, something occurred.
If the condition tried bogusly, nothing occurred.
Regularly, you need
something to happen in any case. For example:
1if species ==
"feline":
2print("Yep, it's
feline.") 3 if species
! =
"feline":
4 print("Nope, not
feline.")
In this model, we have
two if proclamations, one testing for "cat," and another testing for
not-"cat". So, all cases are covered, with some message showing,
contingent upon what the estimation of the variable species
is.
Yet it's more verbose
than needed and somewhat nutty. If the variable species isn't
appointed "cat," at that point obviously it's not "cat." So,
there's no motivation to test for not "cat." The accompanying code is
more compact, but rather more intelligible.
1if species == "cat":
2print("Yep, it's cat.")
3 else:
4 print("Nope, not cat.")
If the
test passes—if the string "feline" has been allocated to the variable
species—the primary message shows. If the test falls flat—if the string "cat" hasn't been assigned
to the variable species—the subsequent
message shows.
Things to take note of:
v
The
watchword else gets its own line and a colon
toward the end.
v
Proclamations
that execute in the else case are indented.
v
As
in this case, quite a few explanations can execute in the else case.
At last, there's elif. It's short for else if. On the off chance
that no test has been effective at this point, and elif
takes a stab at something different.
1if donutcondition ==
"new":
2buyscore = 10
3elif donutprice ==
"low":
4buyscore = 5
5else:
6buyscore = 0
In the example
overhead, if rings are fresh, the score is 10, and Python stops taxing. If they
are not fresh (elif), Python takes a subsequent step, testing for
a coffee price. If the test passes, the score is 5. If that test too flops (else), the score is 0.
- "elif python meaning"
- "elif in python"
- "elif statement in python example"
- "else statement"
- "if elif else python exercises"
- "nested if statement in python"
"compare and contrast if-else and elif statements. give examples"
- "why is it important that if elif and else statements appear in the proper order"
- "shell script to demonstrate if elif else and if statements"
- "What is the difference between if-else and if-elif-else statements"
You can have any number of elif statements. all try a replacement test when all the tests above it have failed. If any elif test succeeds, Python executes any
statements tied thereto and skip any tests that come afterward.
Since an else statement may be a catchall, you'd never
have quite one among them. It always comes last, specifying what occurs if all
tests have failed.
In the example above, we're trying to find just one test to pass. If
donuts are fresh, we do not do a second test, for price. The elif code runs as long as the primary test fails. But sometimes you do not
want to prevent testing after one test passes.
1 buyscore = 0
2 if donutcondition ==
"new":
3 buyscore += 10
4 if donutfilling ==
"chocolate":
5 buyscore += 5
6 if donutprice ==
"sensible":
7 buyscore += 7
The code allows an initial estimation of 0 to the variable buy score. At that point, it makes three tests. Each test that passes expands the estimation of the buy score. If no test passes, the buy score keeps its unique worth, 0.
No comments:
Post a Comment