Questions
Conceptual
Every
if
statement must be followed by a pairedelse
branch. (T/F)Lines contained in an
else
branch in Python do not have to be indented. (T/F)You can name a variable
else
in your program without Python confusing your variable’s name and theelse
keyword. (If you are unsure, this is a good one to try yourself!) (T/F)All subquestions of this problem will refer to this code snippet:
x: int = int(input("Pick a number: ")) y: int = 10 z: int = 2 x = x - 1 if x < 10: print("A") else: if (x % z) == 0: print ("B") if x == (y + z): print("C") else: print("D")
For the following subquestions, answer with a valid input value for x
that would cause the given letter to print. It is OK for your input to cause other letters to print as well. If there is no such value, write “Unreachable”.
4.1 A
4.2 B
4.3 C
4.4 D
Now, answer with a valid input value for x
that would cause the exact given letter(s) to print (no other letters). If there is no such value, write “Unreachable”.
4.5
A
C
4.6
B
C
4.7
C
4.8
D
Write the format of a conditional using the following: You might not need to use all and can use any multiple times:
if
,else
,==
,<condition>
,False
,True
,<do something>
.Write the format of a conditional using the following: You might not need to use all and can use any multiple times:
while
,<condition>
,==
,False
,True
,<do something>
.What does the condition of a conditional have to evaluate to in order to enter its block?
What happens when a return statement is encountered?
All subquestions of this problem will refer to this pseudo code snippet:
if <condition1>:
<do_something>
elif <condition2>:
<do_something>
else:
<do_something>
From the general format of a conditional with an
elif
block, what needs to be True and/or False in order for theelif
block to evaluate?Is
<condition1>
not being met the same as havingif <condition1> == False:
?
All subquestions of this problem will refer to this code snippet:
def main() -> None:
str = "x"
x: str = "y"
y: str = x
z: = x
y = "y"
x
if not(x != y and x != "y"):
print(f"x: {x}")
else:
print("'if' condition not met.")
main()
What is the condition in this code?
What does the condition evaluate to? (Don’t do it in your head, draw a memory diagram!)
What values should x, y, and/or z have to be assigned to in order for the else block to run?
What other values can x, y, and/or z be assigned in order for the
if
block to run?
Solutions
Conceptual Questions - Solutions
F
F
F
4.1 Any value < 11
4.2 Any value >= 11 and odd. (Since
x % z == 0
must be True andz
is 2, this meansx - 1
must be even.)4.3 13
4.4 Any value != 13
4.5 Unreachable
4.6 13
4.7 Unreachable
4.8 Any value >= 11 and even
if <condition> == True:
<do_something>
else:
<do_something>
OR
if <condition>:
<do_something>
else:
<do_something>
while <condition> == True:
<do_something>
OR
while <condition>:
<do_something>
The condition must always evaluate to
True
.The return value is recorded in memory and the function is immediately exited.
<condition1>
must not be met (condition should evaluate toFalse
) AND<condition2>
must be met (condition should evaluate toTrue
).In this case, yes. If we had something like this:
if ("hello" == "hello") == False:
the <condition>
in this case would be everything in between the if
and the :
. In the pseudo-code, we are separating the <condition>
to be separate from the rest (== False
), while in real code the condition is always everything after the if
and before the :
.
not(x != y and x != "y")
The condition evaluates to
True
.To ensure the
else
block runs in the given code, the conditionx != y and x != "y"
must be true. This meansx
should be different fromy
andx
should also be different from the string"y"
. For example, settingx = "a"
andy = "b"
will satisfy this condition, making theelse
block execute.To make the
if
block run, the conditionnot(x != y and x != "y")
must be true, which happens whenx
is either the same asy
or the same as"y"
, or both. In the original code wherex = "y"
,y = "x"
, andz = "x"
, theif
block runs asnot(x != y and x != "y")
evaluates to true.