Search the courses, subjects, tracks, games and features, or jump straight to a page.
5 cards from this section. Quick, good the night before a test.
Write a program that prints whether number = -4 is positive, negative or zero.
Compare the number with 0 twice; whatever is left over must be zero.
What does this program print, and why?
temperature = 18
if temperature > 25:
print("Hot")
elif temperature > 15:
print("Pleasant")
else:
print("Cold")Check the conditions from the top and stop at the first one that is True.
In an if/elif/else chain, what happens after Python finds the first condition that is True?
Think of a chain as a single decision with several possible outcomes.
A chain tests if marks >= 60: (prints C) before elif marks >= 90: (prints A), and its else prints F. What does it print when marks is 95?
Python stops at the first condition that is True.
An electricity company charges 5 per unit for the first 100 units, 7 per unit for units 101 to 200, and 10 per unit above 200. If no units were used, the program should print "No usage this month." Write the bill calculator with an if/elif/else chain and run it for units = 250.
Work out the slab limits first. Each elif only runs when the earlier tests failed, so you never need to test the lower limit of a slab.
Given three side lengths a, b and c, first check whether they can form a triangle (each side must be shorter than the sum of the other two). If they can, print whether the triangle is equilateral, isosceles or scalene; otherwise print "Not a triangle". Use a nested condition and test it with a = 5, b = 5 and c = 8.
Validate first (outer if), then classify (inner if/elif/else), and order the inner tests from most specific to least specific.
6 more questions in this section. Enroll free to practise them all.
Enroll free