
Calvin D. answered 09/26/22
Computer Scientist & Cyber Threat Hunter @ US Air Force
Here's a good way to do it:
I like this approach because it allows me to control when exactly I'm done with the while loop, with the break statement. I never really liked using while loop conditions, but that's my personal preference.
If you want to make the condition be in the while loop instead, we can modify the code slightly:
There's some code duplication here as you can see, as we have to ask for input in two different places. Also, the guess is at the bottom of the while loop - sort of annoying and not very intuitive.
Here's some output:
I left out the play again part - I'll leave that to you as an exercise.