10.14 Exercises

From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period3 exercises3 graded tests

README.md

10.14 Exercises

Read first in the book: 10.5 Finding a File in your Filesystem, 10.6 Using with for Files, 10.7 Recipe for Reading and Processing a File, 10.8 Writing Text Files, 10.9 CSV Format, 10.10 Reading in data from a CSV File, 10.11 Writing data to a CSV File, 10.12 Tips on Handling Files, 10.13 Glossary.

Below are the datafiles that you have been using so far, and will continue to use for the rest of the chapter.

The file below is travel_plans.txt.

The data file travel_plans.txt (each exercise that reads it has a copy in its folder):

This summer I will be travelling.
I will go to...
Italy: Rome
Greece: Athens
England: London, Manchester
France: Paris, Nice, Lyon
Spain: Madrid, Barcelona, Granada
Austria: Vienna
I will probably not even want to come back!
However, I wonder how I will get by with all the different languages.
I only know English!

The file below is school_prompt.txt.

The data file school_prompt.txt (each exercise that reads it has a copy in its folder):

Writing essays for school can be difficult but
many students find that by researching their topic that they
have more to say and are better informed. Here are the university
we require many undergraduate students to take a first year writing requirement
so that they can
have a solid foundation for their writing skills. This comes
in handy for many students.
Different schools have different requirements, but everyone uses
writing at some point in their academic career, be it essays, research papers,
technical write ups, or scripts.

The file below is emotion_words.txt.

The data file emotion_words.txt (each exercise that reads it has a copy in its folder):

Sad upset blue down melancholy somber bitter troubled
Angry mad enraged irate irritable wrathful outraged infuriated
Happy cheerful content elated joyous delighted lively glad
Confused disoriented puzzled perplexed dazed befuddled
Excited eager thrilled delighted
Scared afraid fearful panicked terrified petrified startled
Nervous anxious jittery jumpy tense uneasy apprehensive
Exercise 1

The following sample file called studentdata.txt contains one line for each student in an imaginary class. The students name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student.

The data file studentdata.txt (each exercise that reads it has a copy in its folder):

joe 10 15 20 30 40
bill 23 16 19 22
sue 8 22 17 14 32 17 24 21 2 9 11 17
grace 12 28 21 45 26 10
john 14 32 25 16 89

Using the text file studentdata.txt write a program that prints out the names of students that have more than six quiz scores.

Open the 01 folder and edit main.py. Run Tests checks it.

Exercise 2

Create a list called destination using the data stored in travel_plans.txt. Each element of the list should contain a line from the file that lists a country and cities inside that country. Hint: each line that has this information also has a colon : in it.

Open the 02 folder and edit main.py. Run Tests checks it.

Exercise 3

Create a list called j_emotions that contains every word in emotion_words.txt that begins with the letter "j".

Open the 03 folder and edit main.py. Run Tests checks it.

For teachers

10.14 Exercises

Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 3 Python coding exercises (3 graded).

  • Time: 1 class period
  • Tests: 3 in 3 graded exercises, set up for you when you import the lesson

Reading first

Exercises

FolderGradingTests
01Autograded1
02Autograded1
03Autograded1