Question: PART 2 : Write additional code Update the above code to add the following functionality: 2 a . For each line in lab 3 in

PART 2: Write additional code
Update the above code to add the following functionality:
2a. For each line in lab3in.txt create list of words and print the list. This list may contain duplicates. For
this program, words are groups of characters separated by one or more spaces.
2b. Add the words in the line to a set called words. NOTE: A set is an unordered collection with no duplicate elements. The words are case-sensitive, so for example, 'The' and 'the' will be considered two separate words.
COMP 2087: Programming for Beginners II
FALL 2024 LAB #3
2c. For each line you should also print the contents of the line in uppercase in an output file called lab3out.txt
2d. After all the lines have been processed, write the final value of words in the output file.
Sample output (output file): NOTE: screenshot does not show full set of words
# PART ONE
#import file
infile = open('lab3in.txt','r')
num_e =0 #default #
num_lines =0 #default #
#function
for line in infile:
num_lines = num_lines +1
line = line.rstrip()
count_e = line.count('e')
num_e = num_e + count_e
# print statement for e in line
print(f'line ={line} # of e\'s ={count_e}') # Output format
#find avg
avg = round(num_e/num_lines, 2)
#print statement
print('average # of occurances of "e" per line =', avg)
infile.close()
PART 2 : Write additional code Update the above

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!