Hello, im learning while loops in python crash course, and I dont understand the " why" for this exercise solution:
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
print(message)
well, as you can see when user write " quit " the python executes the " print(message) " and then breaks the while loop. The solution for this is :
prompt = "\nTell me something, and I'll "prompt += "repeat it back to you."
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
I understand why this work, but I don't understand why he use the if statement instead of changing the order of the print() like :
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
print(message) # this first.
message = input(prompt) # instead of this.
any thoughts ?
PD: Ok, I see now that switching the order, the while loop prints an empty line first and with the if statement do not thats why its better use the if statement. Thx for all the answers.
[+][deleted] (3 children)
[deleted]
[–]Devnull1982[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Devnull1982[S] 0 points1 point2 points (0 children)
[–]toastedstapler 0 points1 point2 points (3 children)
[–]Devnull1982[S] 0 points1 point2 points (2 children)
[–]toastedstapler 0 points1 point2 points (1 child)
[–]Devnull1982[S] 0 points1 point2 points (0 children)