• Home
  • Textbooks
  • Python Crash Course: A Hands-On, Project-Based Introduction to Programming
  • Files and Exceptions

Python Crash Course: A Hands-On, Project-Based Introduction to Programming

Eric Matthes

Chapter 10

Files and Exceptions - all with Video Answers

Educators


Chapter Questions

10:06

Problem 3

Guest: Write a program that prompts the user for their name.
When they respond, write their name to a file called guest.txt.

Darren Wilson
Darren Wilson
Numerade Educator
03:06

Problem 4

Guest Book: Write a while loop that prompts users for their
name. When they enter their name, print a greeting to the screen and
add a line recording their visit in a file called guest_book.txt. Make sure
each entry appears on a new line in the file.

Foster Wisusik
Foster Wisusik
Numerade Educator
03:29

Problem 5

Programming Poll: Write a while loop that asks people why they
like programming. Each time someone enters a reason, add their reason
to a file that stores all the responses.

James Kiss
James Kiss
Numerade Educator
06:18

Problem 6

Addition: One common problem when prompting for numerical
input occurs when people provide text instead of numbers. When you
try to convert the input to an int, you’ll get a TypeError. Write a
program that prompts for two numbers. Add them together and print the
result. Catch the TypeError if either input value is not a number, and
print a friendly error message. Test your program by entering two
numbers and then by entering some text instead of a number.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
05:58

Problem 7

Addition Calculator: Wrap your code from Exercise 10-6 in a
while loop so the user can continue entering numbers even if they make
a mistake and enter text instead of a number.

Darren Wilson
Darren Wilson
Numerade Educator
02:32

Problem 8

Cats and Dogs: Make two files, cats.txt and dogs.txt. Store at
least three names of cats in the first file and three names of dogs in the
second file. Write a program that tries to read these files and print the
contents of the file to the screen. Wrap your code in a try-except block
to catch the FileNotFound error, and print a friendly message if a file is
missing. Move one of the files to a different location on your system,
and make sure the code in the except block executes properly.

Foster Wisusik
Foster Wisusik
Numerade Educator
00:12

Problem 9

Silent Cats and Dogs: Modify your except block in Exercise 10-
8 to fail silently if either file is missing.
a string:

Amy Jiang
Amy Jiang
Numerade Educator
01:58

Problem 10

Common Words: Visit Project Gutenberg
(http://gutenberg.org/) and find a few texts you’d like to analyze.
Download the text files for these works, or copy the raw text from your
browser into a text file on your computer.
You can use the count() method to find out how many times a word
or phrase appears in a string. For example, the following code counts
the number of times 'row' appears in a string:

James Kiss
James Kiss
Numerade Educator
10:06

Problem 11

Favorite Number: Write a program that prompts for the user’s
favorite number. Use json.dump() to store this number in a file. Write a
separate program that reads in this value and prints the message, “I
know your favorite number! It’s _____.”

Darren Wilson
Darren Wilson
Numerade Educator
10:06

Problem 12

Favorite Number Remembered: Combine the two programs
from Exercise 10-11 into one file. If the number is already stored,
report the favorite number to the user. If not, prompt for the user’s
favorite number and store it in a file. Run the program twice to see that
it works.

Darren Wilson
Darren Wilson
Numerade Educator
02:42

Problem 13

Verify User: The final listing for remember_me.py assumes
either that the user has already entered their username or that the
program is running for the first time. We should modify it in case the
current user is not the person who last used the program.
Before printing a welcome back message in greet_user(), ask the
user if this is the correct username. If it’s not, call get_new_username()
to get the correct username

James Kiss
James Kiss
Numerade Educator