Conditionals: if, elif and else
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Programs make decisions with if. A condition is a yes-or-no question, such as temp >= 25, and its answer is a bool: True or False. Step through wear.py, which asks for the temperature and picks a T-shirt, a jumper or a coat, and watch which lines run and which are skipped. Run it twice with different answers, then meet the slip every beginner makes: = instead of ==.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01temp holds 18. What does temp >= 25 give?
A comparison always gives a bool. 18 is not greater than or equal to 25, so the answer is False.
02In wear.py you type 30. Which lines print something?
30 >= 25 is True, so the if block runs and prints T-shirt. The rest of the chain is skipped, even though 30 >= 15 is also True. Line 8 is not indented, so it always runs: Bye!
03When does the block under else run?
else has no question of its own. It catches everything the if and elif questions above it did not: it runs only when all of them were False.
04How does Python know which lines belong to an if?
The header ends with a colon, and the block is the lines under it pushed in by the same amount; four spaces is the standard style.
05What happens when you run a file with the line if temp = 25: ?
A single = stores a value and is not allowed as an if condition. Python checks the whole file first, so none of it runs; == is the comparison you meant.
المسرد
- Condition
- A yes-or-no question in code, such as temp >= 25. Python works it out to True or False.
- bool
- The type with exactly two values, True and False. Every comparison gives a bool.
- Comparison
- An operator that compares two values: == (equal), != (not equal), <, <=, > and >=.
- if
- Runs its indented block only when its condition is True.
- elif
- Short for else if: a further question, asked only when every question above it was False.
- else
- The catch-all at the end of a chain: its block runs only when every question above it was False.
- Block
- The indented lines after a line that ends with a colon. They run, or are skipped, together.
- SyntaxError
- The error Python reports when code breaks the language's rules, such as = in place of == in an if. Nothing in the file runs.
الملاحظات والمصادر
- Python has six comparisons: == (equal), != (not equal), <, <=, > and >=. Each one gives True or False, the two values of the type bool.
- Python asks the questions from the top, runs the block of the first one that is True and skips every other block of the chain; the questions below it are not even worked out.
- A block is the indented lines after a colon. Python only needs them to line up; four spaces per level is the standard style (PEP 8). An if with no indented line under it is an IndentationError.
- An if accepts any value, not only True and False: 0, an empty string "" and None count as false, and most other values count as true.
- The figure clears the terminal between the two runs; the $ is the terminal's own prompt. On macOS and Linux the command may be python3. In the real error message the ... is the full path to the file, and the figure wraps its long last line to fit the screen.
- int() stops with a ValueError if the text is not a whole number, such as "warm" or "18.5".
- Python tutorial: More control flow tools (if statements)
- Python reference: The if statement
- Python library: Comparisons
- Python library: Boolean type (bool)
- Python library: Truth value testing
- Python reference: Lexical analysis (indentation)
- Python library: Built-in functions (input, int)
- Python library: Built-in exceptions (SyntaxError, IndentationError)
- PEP 8: Style guide for Python code (indentation)
اختبار يتاح بعد الخطوة الأخيرة.