Python Forum
A little unexpected output from a while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A little unexpected output from a while loop
#1
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)
Reply
#2
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 :)
Reply
#3
(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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 472 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 699 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 504 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,181 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  unexpected output asyrafcc99 0 1,496 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  using 'while loop' output going into infinite loop... amitkb 2 1,941 Oct-05-2020, 09:18 PM
Last Post: micseydel
  sports Stats > table output loop problems paulfearn100 3 2,485 Jul-22-2020, 03:21 AM
Last Post: c_rutherford
  Print output in single file using pramika loop deepakkhw 1 2,063 Jul-11-2020, 11:57 AM
Last Post: j.crater
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,040 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Unexpected output palladium 4 2,728 Jan-11-2020, 03:26 PM
Last Post: palladium

Forum Jump:

User Panel Messages

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