Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this program not print : Welcome to the Jungle message when I enter any name?

Using while loop to quit the program

prompt = "\nTo end this program enter 'x'"
prompt += "\nPlease enter your name: "
message = ''
while message != 'x':
  message = input(prompt)
if message == 'x':
  print("The program has ended......Bye!")
else:
  print( 'Welcome to the Jungle' + message)

Why does this program not print " Welcome to the jungle message when I enter any character other than "x"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your if block is outside the loop, as such it only executes after the while loop exits.

For your usecase, I think that indenting the if block forward by one would work.

prompt = "\nTo end this program enter 'x'"
prompt += "\nPlease enter your name: "
message = ''
while message != 'x':
  message = input(prompt)
  if message == 'x':
    print("The program has ended......Bye!")
  else:
    print( 'Welcome to the Jungle' + message)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading