Puzzles 15 to 18
Explore Python puzzles 15 to 18 that focus on important programming concepts such as if-elif-else branching, negative string indexing, for loops, and defining functions. Learn how the Python interpreter processes these statements and improve your coding skills with immediate feedback through Elo rating updates.
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.
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 - 500 | 46 | -9 |