1
2
3
4
5
6
7
8
9
10
11
12
It wasn't always easy, but you got all the way through. You now know more Python than hundreds of thousands of would-be coders who read but don't practice. It's a major accomplishment. Congratulations.
To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.
Post a review on Amazon for the paperback or the Kindle edition.
Email me to give me a compliment, complaint, or correction. I'll respond.
0,1,2,3,4,5,6,7,8,9,10,11
0
0
0
0
To give the user another chance to input a filename after a FileNotFoundError, you use a _______ ________. (2 words) | while loop | ^ *while loop *$ | |
If there's no error, you end the loop with a _______ statement. | break | ^ *break *$ | |
Code the line that precedes a try that allows the user to try again. | while True: | while `True`: | |
Code the next line. Indent correctly. | while True: | try: | try: |
Code the first three lines, beginning with the while statement. The third line tests whether a is less than b. Pay attention to indents. | while True: try: if a < b: |
^ *while True:\n try:\n if a < b: *$ | |
Code the first four lines, beginning with the while statement. The third line tests whether a divided by b has a remainder of 0. If so, display a message. Make up the message. Pay attention to indents. | while True: try: if a % b == 0: print("No remainder") |
^ *while True:\n try:\n if a % b == 0:\n print\(•.+•\) *$ | |
The next line interrupts the loop if the test passes. Code it. | while True: try: if a % b == 0: print("No remainder") |
break | ^ * break *$ |
The next line deals with the error if the file isn't found. Code it. | while True: try: if a % b == 0: print("No remainder") break |
except FileNotFoundError: | ^ * except FileNotFoundError: *$ |
The next line displays a message if there's an error. Code it. | while True: try: if a % b == 0: print("No remainder") break except FileNotFoundError: |
print("Could not find the file. Please try again.") | ^ * print\(•.+•\) *$ |
In the example that we've been using, type the two lines of code, on separate lines, that are indented one tab. Indent them. | try: except FileNotFoundError: |
^ * try:\n except FileNotFoundError: *$ | |
|
|||