LearnFrenzy
Sign in Take a free test

Conditional Statements · Exercise 3 of 4

Conditional Statements Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Python program has a mistake in its conditions: a syntax error, an exception at run time, or a logical bug such as a wrong branch order. Choose the option that names the real problem.

Question 1 Easy

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
n = 0
if n > 0:
    print("positive")
else if n < 0:
    print("negative")
else:
    print("zero")
A Python has no "else if" on one line; the keyword is elif
B n must be a float
C The comparisons need brackets
D else must come before elif
Question page & discussion
Question 2 Easy

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
score = 72
if score >= 90:
    print("A")
else:
    print("Below A")
elif score >= 70:
    print("B")
A score must be compared with ==
B else must be the last branch; an elif cannot follow it
C 72 is not a valid score
D print needs an f-string
Question page & discussion
Question 3 Medium

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
stock = 0
if stock > 0:
    # TODO: allow ordering
else:
    print("Out of stock")
A Comments are not allowed inside if
B stock must be a bool
C A block must contain at least one statement – a comment is not one; use pass as a placeholder
D else needs a condition
Question page & discussion
Question 4 Medium

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
balance = 500
if balance >= 1000:
    print("Premium")
  else:
    print("Regular")
A else is indented by 2 spaces, which matches no enclosing block; it must line up with its if
B balance must be larger
C else needs a colon after it
D print needs brackets
Question page & discussion
Question 5 Hard

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
QUIT = "q"
command = "x"
match command:
    case QUIT:
        print("Quitting")
    case _:
        print("Other command")
A match needs brackets around command
B case QUIT: is a capture pattern that matches anything, so the case _ after it is unreachable – Python rejects this; use a literal "q" or a dotted name
C QUIT must be lowercase
D There is no error; it prints Other command
Question page & discussion
Question 6 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
marks = 30
if marks >= 40:
    result = "pass"
print("Result:", result)
A NameError: result is assigned only when marks >= 40; on this path it was never created
B SyntaxError: result must be declared
C It prints Result: None
D It prints Result: pass
Question page & discussion
Question 7 Medium

A student with 95 marks should get "Distinction". Which statement correctly identifies the bug?

main.py
marks = 95
if marks >= 40:
    print("Pass")
elif marks >= 75:
    print("First class")
elif marks >= 90:
    print("Distinction")
A elif should be else if
B The broadest condition (>= 40) is tested first, so the more specific branches are never reached; order them from highest to lowest
C marks must be a float
D The last branch needs else
Question page & discussion
Question 8 Easy

Exactly one label should be printed for a temperature. Which statement correctly identifies the bug?

main.py
temp = 45
if temp > 40:
    print("Hot")
if temp > 25:
    print("Warm")
else:
    print("Cold")
A 45 is out of range
B else should be elif
C The second if starts a new, independent test; it should be elif so that only one branch runs
D print must be called once
Question page & discussion
Question 9 Medium

The user types 1 but the program prints "Invalid choice". Which statement correctly identifies the bug?

main.py
choice = input()
if choice == 1:
    print("Starting game")
else:
    print("Invalid choice")
A input() needs a prompt
B == cannot compare numbers
C else needs a condition
D input() returns the str "1", and "1" == 1 is False; compare with "1" or convert with int()
Question page & discussion
Question 10 Easy

Anyone aged 18 or more may vote, but an 18-year-old is told "Not eligible". Which statement correctly identifies the bug?

main.py
age = 18
if age > 18:
    print("Eligible to vote")
else:
    print("Not eligible")
A age must be a string
B else should be elif age < 18
C > excludes 18 itself; the condition should be age >= 18
D The message is spelled wrong
Question page & discussion
Up next · Exercise 4Conditional Statements Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links