Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you will practice: reading from files, creating and accessing lists, and handling exceptions in Python. Write a program that allows the user

In this assignment you will practice: reading from files, creating and accessing lists, and handling exceptions in Python.

Write a program that allows the user to navigate through the lines of text in any text file. The program prompts the user for a filename and copies the lines of text from the file into a list. The program then prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the text in that line number.

Input validation: You must print a friendly error message and allow the user to try again if the program cannot open the requested file, and whenever the user enters an invalid line number.

Here is a sample run (user input was changed to red for improved readability):

Enter the input file name: quotes.xtxt I cannot open the file quotes.xtxt - Please check the name and try again. Enter the input file name: quotes.txt The file has 16 lines. Enter a line number [0 to quit]: 1 1 : Hakuna Matata! Enter a line number [0 to quit]: -1 Try again. Line number must be between 0 and 16 Enter a line number [0 to quit]: 16 16 : Just keep swimming! Enter a line number [0 to quit]: 17 Try again. Line number must be between 0 and 16 Enter a line number [0 to quit]: 5 5 : It's kind of fun to do the impossible. Enter a line number [0 to quit]: A Try again. Please enter a number between 0 and 16 Enter a line number [0 to quit]: $ Try again. Please enter a number between 0 and 16 Enter a line number [0 to quit]: 4 4 : To infinity and beyond! Enter a line number [0 to quit]: 0

quotes.txt

Hakuna Matata! All our dreams can come true, if we have the courage to pursue them. If you can dream it, you can do it. To infinity and beyond! It's kind of fun to do the impossible. Venture outside your comfort zone. The rewards are worth it. The way to get started is to quit talking and begin doing. We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths. You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you. You can design and create, and build the most wonderful place in the world. But it takes people to make the dream a reality. When you believe in a thing, believe in it all the way, implicitly and unquestionable. When you're curious, you find lots of interesting things to do. Disneyland will never be completed. It will continue to grow as long as there is imagination left in the world. If you don't think, then you shouldn't talk. The only thing predictable about life is its unpredictability. Just keep swimming!

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

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions