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"?

>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)

Leave a Reply