A Smarter Way to Learn Python / Chapter 77 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

11

12

 

You did it!

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.


  All of my books include free interactive online exercises that make the knowledge stick.

A Smarter Way to Learn JavaScript
Shop for it at Amazon
A Smarter Way to Learn HTML and CSS
Shop for it at Amazon
A Smarter Way to Learn jQuery
Shop for it at Amazon

copyright © 2017 by Mark Myers. All rights reserved.

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: *$
  1. In IDLE code a Python program that asks the user to input a file name, using a while loop to give her another chance to input if there's a FileNotFoundError.
  2. If the file is found, load it. If the file isn't found, display a message.
  3. Save the Python program to the Desktop and run it.
  4. As the user, input a filename for a file that doesn't exist. Then input a filename for a file that does exist.