Menu
CoddyTech

elif and Value Selection

Lesson 10 of 48 in Coddy's Python for Luau Developers course.

Python elif replaces Luau elseif and its suites use colons and indentation. Both languages also have real conditional expressions: Luau writes if condition then a else b, while Python writes a if condition else b. These preserve a selected false or zero value, unlike an and/or substitute in cases where the chosen value is falsy. Order branches so the first matching rule is the intended one.

Luau example

local score = 8
local result
if score >= 10 then result = "high"
elseif score >= 5 then result = "mid"
else result = "low" end
print(result)

Python

score = 8
if score >= 10:
    result = "high"
elif score >= 5:
    result = "mid"
else:
    result = "low"
print(result)

Porting trap

Use Python colons and indentation rather than Luau then/end delimiters; braces are not control-block delimiters in either language.

Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.

challenge icon

Challenge

Beginner

Read 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__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Luau Developers

Practice on your own: Online Python compiler