Nested If Statements
By nesting if statements, you can create more complex decision-making logic. It allows you to handle multi-level conditions and execute specific code blocks based on the combination of those conditions. This nesting can be done with if statements, and elif statements. In this example we’ve nested the elif and if statements.
weight = float(input(“Enter the weight kilograms: “))
destination = input(“Enter the destination: “)
if weight <= 0:
print(“Invalid weight.”)
elif destination == “domestic”:
if weight <= 1:
print(“Shipping cost: $5”)
elif weight <= 5:
print(“Shipping cost: $10”)
elif weight <= 10:
print(“Shipping cost: $15”)
else:
print(“Shipping cost: $20”)
elif destination ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access