Puzzles 15 to 18
Examine branching, negative indices, for loop, and functions in Python.
We'll cover the following...
Puzzle 15
What is the output of the following code?
Note: Enter the output you guessed. Then, press the Run button, and your new Elo will be calculated. Don’t forget to save your Elo rating.
############################### id 148## Puzzle Elo 845## Correctly solved 36 %#############################def if_confusion(x, y):if x > y:if x - 5 > 0:x=yreturn "A" if y == y + y else "B"elif x + y > 0:while x > y: x -= 1while y > x: y -= 1if x == y:return "E"else:if x - 2 > y - 4:x_old = xx=y*yy = 2 * x_oldif (x - 4) ** 2 > (y - 7) ** 2:return "C"return "D"return "H"print(if_confusion(3, 7))
Enter the input below
Branching
Just like any other programming language, Python also has Conditional Statements also known as Branching Statements. To create branches, you can use the keywords if
, else
, or elif
.
The computer does not execute the code strictly from top to bottom, and you shouldn’t as well.
Instead, start where the program execution starts, which is at the bottom with the function call if_confusion(3, 7)
.
Now you know that x=3
and y=7
. Then, you proceed to do what the interpreter does.
As x>y
is false
, you can skip the whole upper part of the function. Similarly, you can skip the if
branch for x-2>y-4
. It’s easy to see that the function returns 'H'
.
Solution
The correct solution is H
.
Your Elo rating will be updated according to the following table.
Your Elo | Correct | Incorrect |
---|---|---|
0 - |