 # 4.2 Question; first time poster

#1

I am trying to understand why the last question in 4.2 “the code runs but doesn’t print anything”, can anyone please explain why?

num = 7

if num % 2 == 0:
if num % 2 == 1:
print(“odd”)

#2

After the program assigns 7 to the num variable, it runs num through the first if-statement but num doesn’t meet the requirement (num % 2 == 1). The program then skips the rest of the if statement and just ends the program. The second if-statement is nested so if the first if-statement doesn’t return a value of true, the program won’t even try the second if-statement, even if the second one would return a value of true. It just terminates.

Hope that helps.

#3

Hi Katbax,

Thank you, I think i understand. (Although I think you had a typo in your parenthesis?)

What I am understanding is that is you have a nested if-statement, if the first one is false, then the program ends right there

What would you need to do to the program in order for it to actually print (“odd”)…? What are some variations that I can try in repl, because I’m trying a few different things but none seem to work

#4

Yes, you are correct. When I wrote num % 2 == 1, I was trying to show that while num = 7, the value of num % 2 is equivalent to 1, not 0. To get the program to print “odd”, try commenting out or removing the first if-statement so the program goes directly to the second if-statement. The code will never print anything in its current state as there is no number that would return true for both if-statements. You could also try assigning num to an even number, then adding a line of code after the first if-statement (but above the second if-statement) to re-assign num to an odd number.

#5

I would use else to tell it to print odd when the statement is false

#6

Because the condition in the 1st (“outer”) if statement is false, the block inside it won’t execute. So, the 2nd if statement won’t even be evaluated.

To fix it, you will want to remove the 1st if statement completely and then decrease the indent on the “inner” if and its code block (the print statement).