Flip Again?¶
In this program, you’ll see how using a “while True and break” (post-test) loop might be better than a regular while
loop.
Files Needed¶
What You Should See¶
The code I have provided does not run. Once you fix it, it will look roughly like this.
You flip a coin and it is... TAILS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? n
What You Should Do on Your Own¶
Assignments turned in without these things will receive no credit.
The code as given does not run. Notice that the
while
tests ifagain == "y"
, but the variableagain
doesn’t have a string value at first. Give it a string value so that the code will not encounter a run-time error and the loop will run at least once.Now that program is working, change the loop from a pre-test
while
loop to a post-test “while-true” loop. Make sure it still works.What happens if you delete the
again = "y"
line right before the loop? Does the program still work? Why or why not? (Answer in a comment.)
©2021 Daniel Gallo
This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Adapted for Python from Graham Mitchell’s Programming By Doing