Python Forum

Full Version: How does while-If-elif-else-If loop conditions check run
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

This is a working hangman code, I am struggling to understand how it work,
hope someone here can help me to clarify the 3 questions I have below so I understand the while loop better.

The following codes is an image of the codes:

[Image: 0Y8vAHWHV]
By referring to the above code, I have 3 basic loop questions below:

1) The program start by checking the first IF, if this doesn't match it will go to the next ELIF and if that also doesn't match it will go to the ELSE.
Regardless of what's the outcome of the first 3 conditions, the code will run the last IF statement.

2) Let's just say if the first IF condition meets, it will then execute the following and then repeat the while loop again, is that correct?
if letter.upper() in lstGuessed:
letter = ''
print("Already guessed!!")


3) The only time when the program will exit the while loop is when the last IF statement matches, correct?

Thanks in advance!

  1. It will check the outcome of the last if regardless of the outcome of the if/elif/else block
  2. It will check the outcome of the last if, if True it will break out of the loop otherwise it will repeat the loop
  3. Yes
Thanks @Yoriz