Ternaries and elif
Lesson 10 of 48 in Coddy's Python for Dart Developers course.
Dart uses else-if chains and a condition-first ternary expression. Python uses elif for the chain and places the condition between the result alternatives in its conditional expression. Only the selected expression is evaluated. Order overlapping conditions carefully because a chain executes its first matching branch. Dart requires the condition to be bool; Python uses its truth-testing rules. Avoid a logical and/or chain as a substitute when the true result can be false-valued, such as zero. Python conditional expressions preserve the chosen result regardless of whether that result is truthy.
Dart
void main() {
final ready = true;
print(ready ? 0 : 5);
}Python
ready = True
print(ready and 0 or 5)
print(0 if ready else 5)Porting trap
Do not paste Dart's question-mark and colon ternary syntax into Python. Rewrite the whole expression in Python's order.
Challenge
BeginnerRead an integer order total in cents and a membership token, either yes or no. Totals are nonnegative. Shipping is 0 for totals at least 5000, otherwise 200 for members, otherwise 500. Print shipping on line one and free if shipping is zero or paid otherwise on line two. The high-total rule takes priority.
Print only these lines, ending each with a newline. Keep the supplied input scaffold; it reads the stated values without prompts.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler