Branches and Expressions
Lesson 10 of 48 in Coddy's Python for JavaScript Developers course.
Express ordered decisions with one branch chain and choose a value with a conditional expression.
JavaScript
const score = 73;
let result;
if (score >= 80) { result = "high"; }
else if (score >= 50) { result = "pass"; }
else { result = "retry"; }
console.log(result);
console.log(score >= 50 ? "ready" : "practice");Python
score = 73
if score >= 80:
result = "high"
elif score >= 50:
result = "pass"
else:
result = "retry"
print(result)
print("ready" if score >= 50 else "practice")What changes
Both programs print pass and ready. Python uses elif for another condition in the same chain and else for the remaining case. Headers align, end in colons, and control indented blocks. Only the first true branch runs. Test a more restrictive threshold before a broader one when both could match. The comparison >= includes the boundary itself. A conditional expression has the order value_if_true if condition else value_if_false. It evaluates only the selected value and is useful for one short choice.
Common mistake
Separate if statements are separate decisions, so several can run and overwrite a result. An elif chain stops once it has selected a branch. Do not copy JavaScript question-mark syntax into Python or reverse the two values in the Python expression.
Your task
Apply shipping rules in priority order. Use a branch chain for the rate and a short conditional value for its label, checking the exact free-shipping boundary.
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 JavaScript Developers
1From JavaScript to Python
Run Your First Python ProgramPrint, Comments, and StringsVariables Without let or constNumbers and ConversionBlocks Use IndentationRecap: Rewrite a Receipt4Iteration and Transformations
for Loops Iterate ValuesIndexes and Dict Iterationwhile, break, and continueComprehensions and SortingRecap: Build a Ranking2Values and Decisions
None, True, and FalseEquality and IdentityTruthiness and Short-CircuitBranches and ExpressionsRecap: Eligibility Rules3Collections and References
Lists Instead of ArraysTuples and UnpackingDictionaries and RecordsSets, Aliasing, and CopiesRecap: Clean an Inventory6Reliable Python Programs
Input and Output ContractsExceptions and ValidationModules and the Main GuardReading Type HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler