Checkpoint 5.9.1.
- I only
- You can not use a Boolean expression after an else.
- II only
- Yes, II will give the same result.
- III only
- No, III will not give the same result. The first if statement will be true, but the second will be false, so the else part will execute.
- II and III
- No, Although II is correct III will not give the same result. Try it.
- I, II, and III
- No, in I you can not have a Boolean expression after an else.
Which of I, II, and III below gives the same result as the following nested if?
# nested if-else statement
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
else:
if x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
I.
if x < 0:
print("The negative number ", x, " is not valid here.")
else (x > 0):
print(x, " is a positive number")
else:
print(x, " is 0")
II.
if x < 0:
print("The negative number ", x, " is not valid here.")
elif (x > 0):
print(x, " is a positive number")
else:
print(x, " is 0")
III.
if x < 0:
print("The negative number ", x, " is not valid here.")
if (x > 0):
print(x, " is a positive number")
else:
print(x, " is 0")