Python Forum
A little unexpected output from a while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: A little unexpected output from a while loop (/thread-11563.html)



A little unexpected output from a while loop - linuxnoob - Jul-16-2018

I'm not sure why this program continues to print 'quit' when the user tells the program to quit. That last if statement is inside the while loop, it's supposed to check if the input is not equal to 'quit'. Even if it is equal to quit there is still an output. Reading through the code again, shouldn't it skip the while loop completely?

prompt = input("Tell me something, and I will repeat it back to you: ") 
prompt += "\nEnter 'quit' to end the program. "

message = ""
while message != 'quit':
	message = input(prompt)
	
	if message != 'quit':
		print(message)



RE: A little unexpected output from a while loop - Zombie_Programming - Jul-16-2018

First off, when you initialize prompt, you set it to whatever the user enters. Then you add to the user's input prompt += "]nEnter 'quit' to end the program". This makes your user input not what you intend what it to be. A fix for this is to print to the screen print("Enter quit to end the program"), then set prompt to get the user's input. This way the message that tells the user that they can enter "quit" to end the program shows before the user is asked to input anything. Next, you want to set your message variable to be equal to prompt. This way your message contains what the user input into the console.

Your while loop alone is fine, however, if your message doesn't equal quit, you what to print the message to the screen and then ask the user again for more input and reset the message variable. This is as simple as just copying your code before the while loop, then pasting it into where your message doesn't equal quit.

Also the if message != 'quit': is redundant. Your while loop is already checking this condition, no need to repeat it.

Hopefully, this helps somewhat!

Zombie Programming :)


RE: A little unexpected output from a while loop - linuxnoob - Jul-16-2018

(Jul-16-2018, 01:56 AM)Zombie_Programming Wrote: First off, when you initialize prompt, you set it to whatever the user enters. Then you add to the user's input prompt += "]nEnter 'quit' to end the program". This makes your user input not what you intend what it to be. A fix for this is to print to the screen print("Enter quit to end the program"), then set prompt to get the user's input. This way the message that tells the user that they can enter "quit" to end the program shows before the user is asked to input anything. Next, you want to set your message variable to be equal to prompt. This way your message contains what the user input into the console.

Your while loop alone is fine, however, if your message doesn't equal quit, you what to print the message to the screen and then ask the user again for more input and reset the message variable. This is as simple as just copying your code before the while loop, then pasting it into where your message doesn't equal quit.

Also the if message != 'quit': is redundant. Your while loop is already checking this condition, no need to repeat it.

Hopefully, this helps somewhat!

Zombie Programming :)

Weird. I'm entering it word for word how the book says to do it. Maybe I should have picked a better book! Thank you for your help.


RE: A little unexpected output from a while loop - gontajones - Jul-17-2018

Here the output was:

Output:
Tell me something, and I will repeat it back to you: GontaJones GontaJones Enter 'quit' to end the program. No No GontaJones Enter 'quit' to end the program. Nope Nope GontaJones Enter 'quit' to end the program. quit
And the script ends normally.