QuestionQ162

VerifiedControl Flow – Conditional Blocks and Loops

Drag the conditional expressions to form code that outputs * on the screen. Some code boxes will not be used.

Drag & Drop
pool = 42 - 1 // 2

if :
    print("*")
elif :
    print("**")
else:
    print("***")
Explanation

The code prints one asterisk when pool is positive, two when negative, and three when zero, using an if/elif/else chain. To output a single '*', the first branch must be true for a positive value, so condition1 is 'pool > 0'; the second branch must catch negatives, so condition2 is 'pool < 0', leaving zero for the else. The distractors 'pool => 0' and 'pool = 0' are invalid Python: '=>' is not a comparison operator and '=' is assignment, so both raise a SyntaxError and cannot appear in working code. Only the two valid relational operators are used, which is exactly what this PCEP item tests.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!