bhylu.com

Advertisements

Python Else

else handles the case where an if condition did not pass. It completes a two-way decision without repeating the opposite condition.

Understand the idea

The else belongs to its matching if at the same indentation level. Choose output that explains the actual rule rather than a vague success or failure label. If invalid input is a separate case, validate it rather than accidentally treating it as an ordinary negative outcome.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

requested = 3
available = 2
if requested <= available:
    print("Seats reserved")
else:
    print("Not enough seats")

Read the result

The result is Not enough seats because the requested count exceeds availability.

Try one change

Reject non-positive requests before comparing the requested and available counts.