Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS QUESTION IS AVAILABLE ONLINE PLEASE VIEW LINK BEFORE POSTING SOLUTION:http:

ID: 3594596 • Letter: T

Question

THIS QUESTION IS AVAILABLE ONLINE PLEASE VIEW LINK BEFORE POSTING SOLUTION:http://www.codestepbystep.com/problem/view/python/fileio/flip_lines

Write a function named flip_lines that accepts as its parameter a string representing a file name, opens that file and reads its contents as a sequence of lines, and writes to the console the same file's contents with successive pairs of lines reversed in order, with alternating capitalization. For example, if the input file named carroll.txt contains the following text:

Then the call of flip_lines("carroll.txt") should print the first pair of lines in reverse order, then the second pair in reverse order, then the third pair in reverse order, and so on. It should produce the following console output:

Notice the alternation between all-uppercase and all-lowercase. Also note that a line can be blank, as in the third pair. An input file can have an odd number of lines, as in the one above, in which case the last line is printed in its original position. You should not make any assumptions about how many lines are in the file.

If the input file does not exist or is not readable, your function should instead pra message in exactly the following format:

Constraints: Your solution should read the file only once, not make multiple passes over the file data.

Explanation / Answer

def flip_lines(filename):
try:
fh = open(filename)
except IOError:
print("Unable to open input file " + filename + "!")
return
lines = []
with fh:
lines = fh.readlines()
if len(lines) == 0:
return
i = 0
while True:
line1 = ''
if i < len(lines):
line1 = lines[i].rstrip()
else:
break
line2 = ''
if i + 1 < len(lines):
line2 = lines[i+1].rstrip()
else:
print(line1)
break

i = i + 2
print(line2.upper())
print(line1.lower())

flip_lines('carroll.txt')

# copy pstable code link: https://paste.ee/p/Iuia3