I need help with the following programing exercise using Python 3.2 Assume that
ID: 3538243 • Letter: I
Question
I need help with the following programing exercise using Python 3.2
Assume that a file containing a series of names ( as strings ) is named names.txt and exists on the computers disk. Design a program that displays the numbers of names that are stored in the file. ( Hint: Open the file and read every string stored on it. Each time you read a string, increment a counter variable. When you've read all the strings from the file, the counter variable will contain the number of names stored in the file.)
Explanation / Answer
#assumes names separated by space
counter=0
with open('names.txt') as a_file:
for a_line in a_file:
s=a_line.rstrip().split()
counter=counter+len(s)
print("Number of names: "+str(counter))