1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ArbitrLikvidat [17]
4 years ago
7

Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename

and input the lines of text into a list. The program then enters a loop in which it 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 line associated with that number.
It should be like as this sample run follows.

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
Computers and Technology
1 answer:
pickupchik [31]4 years ago
7 0

Answer:

Explanation:

iname=input("Enter the file name: ")

inputfile=open(iname,'r')

lines=[]

for line in inputfile:

lines.append(line)

inputfile.close()

print("The file has ",len(lines)," lines")

while True:

linenumber=int(input("Enter the line number or 0 to quit: "))

if linenumber==0:

break

elif linenumber > len(lines):

print("Error: line number must be less than ", len(lines))

else:

print(linenumber, " : ", lines[linenumber - 1])

You might be interested in
How can I get multiple user inputs in Java ? I want to be able to use it for subtraction, addition,division and multiplication.F
Evgesh-ka [11]
You can do something like this :

Scanner sc = new Scanner(System.in);
int[] nums = new int[4];

for(int i = 0; i < nums.length; i++) {
System.out.println("Enter next number: ");
nums[i] = sc.nextInt();
7 0
2 years ago
Dragging a mouse over text is called
Fantom [35]

Answer:

typing

Explanation:

8 0
3 years ago
Deborah believes that people of other nationalities are inferior to her. She treats them with disdain and does not miss an oppor
belka [17]
I recommend asking another category, not sure which one but computers and technology is not a good start. Keep looking and good luck!
3 0
3 years ago
Explain how low atmospheric oxygen level affected alligator growth in terms of cellular respiration
geniusboy [140]

Answer: Hyperoxic alligators show significantly lower breathing rates than their siblings in normoxic and hypoxic groups. Conversely, each breath supports greater oxygen consumption in hyperoxic animals than in other groups. Statistical significance between groups was calculated by ANOVA with post hoc Tukey–Kramer

Explanation:

4 0
3 years ago
Does the child in the crosswalk have the right of way? Explain why or why not.
qwelly [4]
A pedestrian always has the right of way when crossing a street in a crosswalk
3 0
3 years ago
Other questions:
  • Consider the following business environment.
    14·1 answer
  • What are some ways to change column width? Check all that apply.
    13·2 answers
  • Write a program which capitalize every character after full stopin a given sentence
    11·1 answer
  • Match the metric units with these measurements
    8·1 answer
  • Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'. T
    15·1 answer
  • Write a program code to accept the names of 3 users and generate a user name as shown in the example. Accept the name of 3 user
    9·1 answer
  • What are the purposes of using a text-to-speech tool? Check all that apply.
    9·2 answers
  • Dash transfers several bits of data together at one time<br>​
    6·1 answer
  • Determine what is printed by the following code.
    8·1 answer
  • Hey, My paperclips just stoped producing, does anyone have any idea how to start them up again?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!