Python: Jupiter Exercise 5 Open the file romeo.txt and read it line by line. For
ID: 3757099 • Letter: P
Question
Python: Jupiter
Exercise 5 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split0 method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order. Desired output: ['Arise', 'But', 'It', 'Juliet', "Who', 'already, 'and', breaks', 'east, 'envious', 'fair, 'grief', 'is', 'kill', 'light', 'moon', 'pale', sick', soft', 'sun', 'the', through', 'what', 'window', 'with', 'yonder] First, write the pseudocode for your program here. In Pseudocode here (do not run the cell) Now write the Python code. We haven't talked about files yet, so we'll get you started with the code for opening the file In fname romeo.txt" # open the file fh-open (fname) # read the lines in the file into a list flines-fh.readlines ) # your code goes here, if necessary for line in flines: # your code goes here, if necessary # your code goes here, if necessaryExplanation / Answer
fname = "romeo.txt" fh = open(fname) flines = fh.readlines() words = [] for line in flines: for word in line.strip().split(): if word not in words: words.append(word) words.sort() print(words)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.