A Smarter Way to Learn Python / Chapter 76 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

11

12

 

Congratulations. You've aced all the exercises for this chapter.


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


Fill in the blank. ______
  filename = input("What text file to open? ")
  with open(filename) as f:
    print(f.read())
except FileNotFoundError:
  print("Sorry, " + filename + " not found.")
try:^ *try: *$
Fill in the blank. try:
  filename = input("What text file to open? ")
  with open(filename) as f:
    print(f.read())
_______ FileNotFoundError:
  print("Sorry, " + filename + " not found.")
except^ *except *$
Fill in the blank try:
  filename = input("What text file to open? ")
  with open(filename) as f:
    print(f.read())
except __________________
  print("Sorry, " + filename + " not found.")
FileNotFoundError: File`Not`Found`Error`:
Fill in the blank. try:
  filename = input("What text file to open? ")
  with open(filename) as f:
    print(f.read())
________________________
  print("Sorry, " + filename + " not found.")
except FileNotFoundError:except FileNotFoundError:
Code the first two lines of a block with exception handling. The second line opens a JSON file for writing. Assume the json module has already been imported. Make up what you need to. try:
  with open("xyz.json", "w") as j:
^ *try:\n with open\(•.+\.json•, •w•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Code the next line to deal with the file not being found. try:
  with open("xyz.json", "w") as j:
except FileNotFoundError: ^ *except FileNotFoundError: *$
Code the next line. It displays a message. Make up the message. Remember to indent. try:
  with open("xyz.json", "w") as j:
    json.dump(some_list, j)
except FileNotFoundError:
  print("Oh no!") ^ * print\(•.+•\) *$
Code the first two lines of a block with exception handling. The second line opens a text file for reading. Use shorthand. Make up what you need to. try:
  with open("xyz.txt") as f:
^ *try:\n with open\(•.+\.txt•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$
Code the next line to display a message. The message is stored in a variable. Make up the variable name. Remember to indent. except:   print(error_message) ^ * print\([a-zA-Z_][a-zA-Z0-9_]*\) *$
Code five lines that attempt to open a JSON file and read it. If the file doesn't open, display a message. Use shorthand. Make up what you need to. Assume that the json module has already been imported. The third line begins with a double indent. try:
  with open("doctors.json") as j:
    j_content = json.load(j)
except FileNotFoundError:
  print("The file cannot be found.")
^ *try:\n with open\(•.+\.json•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = json\.load\(\1\)\nexcept FileNotFoundError:\n print\(•.+•\) *$
  1. In IDLE code a Python program that tries to open a text file for reading. Ask it to open a file that doesn't exist.
  2. If the file isn't found, display a message.
  3. Save the Python program to the Desktop and run it.