LESSON 01 – READING FROM A FILE

 


LESSON WORK

PROGRAM 1 – CHARACTER COUNT

Write a program that reads a file and counts how many characters are in that file.  Make sure to test your program on a file with few characters to make sure it works correctly. 

Note: I am not sure if the character that forces the cursor to the next line is going to be counted.

 

PROGRAM 2 – A COUNT

Write a program (or alter your previous program) to count how many occurrences of the letter A (lower case or upper case) appear in the file.

PROGRAM 3 – NAME COUNT

Write a program that asks the user for a name.  It then opens a text file and checks how many times that name appears in the file. 

 

It is up to you to determine what counts as a match.  For example, if the user entered Trump, should the word trumped count as a match?  You decide.  You should include your matching criteria as a comment at the top of your program.

 


Try running the following programs to determine how the find and count functions work on a string.

 

 

PROGRAM #1

 

name = "Patrick Campeau"

letter = input("Enter a letter")

print(name.find(letter))

 

 
PROGRAM #2
 
s = "This be a string"
if s.find("is") == -1:
    print("No 'is' here!")
else:
    print("Found 'is' in the string.")

 

 
PROGRAM #3
 
name = "Patrick Campeau" 
print(name.count("a"))
print(name.count("c")) 
print(name.count("z"))
 
Note: Thank you to Dylan T for putting out the count function.