Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assistance re-working this program. Needs to have a while loop. 1- use a while loop 2- can not use tabs 3- cannot use format def

Assistance re-working this program. Needs to have a "while" loop.

1- use a while loop

2- can not use tabs

3- cannot use format

def extract_lines(file_name, num_lines):

with open(file_name, 'r') as file: lines = file.readlines() total_lines = len(lines) if abs(num_lines) > total_lines: return f"Number of lines in the file: {total_lines}" if num_lines > 0: return lines[:num_lines] else: return lines[num_lines:]

def main(): print("Welcome to heads or tails file operations") print("This program accepts a file name and the number of lines.") print("A positive number of lines displays from the beginning of the file.") print("A negative number of lines displays from the end of the file.") file_name = input("Enter the file name: ") num_lines = int(input("Enter a positive or a negative integer: ")) lines = extract_lines(file_name, num_lines) if type(lines) == list: if num_lines > 0: print(f" Below is the first {num_lines} lines for the file: {file_name}") else: print(f" Below is the last {abs(num_lines)} lines for the file: {file_name}") for line in lines: print(line.strip()) else: print(lines)

if __name__ == '__main__': main()

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Introduction The purpose of this assignment is to familiarize you with the writing Python scripts that make use of using files. Heads or Tails Write a program that will prompt the user to enter a file name and the number of lines to extract from that file name. When the number of lines is positive, the program will display the lines from the beginning of the file. When the number of lines is negative, the program will display the lines from the end of the file. If the number of lines exceeds the total number of lines in the file, then only the line count should be displayed. Program Input The program should prompt the user in this order (bold type is what the user entered): Enter the file name: lines10.txt Enter a positive or a negative integer: 4 Program Calculation The program will calculate where to start displaying the number of lines. If the user enters a number that exceeds the actual number of lines of the file, the program should display the number of lines in the file. Program Output Shown below is the expected output produced by the program: Welcome to heads or tails file operations This program accepts a file name and the number of lines. A positive number of lines displays from the beginning of the file. A negative number of lines displays from the end of the file. Enter the file name: lines10.txt Enter a positive or a negative integer: 4 Below is the last 4 lines for the file: lines10.txt Line 7 Line 8 Line 9 Line 10 >> Welcome to heads or tails file operations This program accepts a file name and the number of lines. A positive number of lines displays from the beginning of the file. A negative number of lines displays from the end of the file. Enter the file name: lines10.txt Enter a positive or a negative integer: 3 Below is the first 3 lines for the file: lines10.txt Line 1 Line 2 ppy 3 Your output should look like the output above and everything should align correctly You are required to use functions for this assignment. Programming Hints - It may be helpful to count the number of lines in the file first. - The program may need to import os.path and linecache - A check of the length of the file name should be done and the program should continue to prompt until the file name is greater than 3 characters. Example: while len(name)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions