Menu
CoddyTech

If Expressions and Elif

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

Kotlin if can directly produce a value with if (ready) 0 else 5. Python writes 0 if ready else 5. Python elif chains mutually exclusive branches; independent if statements can all run. Both conditional forms evaluate only the selected result expression. A zero result is preserved by the conditional even though Python considers zero false.

Kotlin

fun main() {
    val ready = true
    println(if (ready) 0 else 5)
}

Python

ready = True
print(0 if ready else 5)

Porting trap

Python ready and 0 or 5 does not preserve a selected zero. Use the conditional form rather than combining truth tests to simulate Kotlin if expressions.

Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.

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 Kotlin Developers

Practice on your own: Online Python compiler