Python Forum
How does while-If-elif-else-If loop conditions check run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does while-If-elif-else-If loop conditions check run
#1
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!
Reply
#2

  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
Reply
#3
Thanks @Yoriz
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to automate loop test check on Network device jpc230 1 580 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,786 Nov-23-2020, 11:01 PM
Last Post: perfringo
  simple if elif conditions in Python Chandan 2 2,065 Jan-08-2020, 02:36 PM
Last Post: DeaD_EyE
  MySql - Loop - CHeck File gcclinux 1 2,084 Nov-30-2019, 11:51 AM
Last Post: ThomasL
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,948 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,237 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  3 conditions to check and return ilcaa72 3 3,190 May-24-2018, 03:13 AM
Last Post: ilcaa72
  for loop with 2 conditions vaison 9 6,122 Apr-19-2018, 01:28 PM
Last Post: vaison
  How do you check if something exists or doesn't in the conditions of an if? Klar 1 2,506 Dec-15-2017, 03:47 PM
Last Post: sparkz_alot
  applying 2 conditions to a loop Pedroski55 1 2,842 Nov-08-2017, 07:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020