Question

Write a program that asks the user for the name of a file. The program should...

Write a program that asks the user for the name of a file.

The program should display only the first five lines of the file’s contents.

If the file contains less than five lines, it should display the file’s entire contents. by python using while loop

Homework Answers

Answer #1

PYTHON CODE :

file = open("input.txt","r") # open the file
sentence = file.readlines()
i = 0
for ln in sentence: # reading line by line
if i < 5:
print((i + 1), ln.strip())
i = i + 1

INPUT :

OUTPUT :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
11. First and Last Design a program that asks the user for a series of names...
11. First and Last Design a program that asks the user for a series of names (in no particular order). After the final person's name has been entered, the program should display the name that is first alphabetically and the name that is last alphabetically. For example, if the user enters the names Kristin, Joel, Adam, Beth, Zeb, and Chris, the program would display Adam and Zeb. I need help with creating a python code for this using a sentinel...
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
Design a program that asks user to enter any three numbers. The program should display the...
Design a program that asks user to enter any three numbers. The program should display the three numbers in ascending order. using python code please
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
in c++ Write a program that asks the user for the speed of a vehicle (in...
in c++ Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. It should use a loop to display the total distance traveled. The speed traveled should be limited by the fastest speed achieved by a car thus far. Values should not be negative.•Ex: if hours = 3 and speed = 40, then the program should display•Hour 1: Distance Traveled: 40 miles•Hour 2: Distance Traveled: 80...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Write the function definition for a function which asks the user for their name and then...
Write the function definition for a function which asks the user for their name and then prints a personalized greeting. DO NOT WRITE AN ENTIRE PROGRAM. DO NOT WRITE THE STATEMENT THAT CALLS THE FUNCTION JUST WRITE THE FUNCTION DEFINITION
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”....
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”. If the user enters “bus” display “$1.00”. If they enter “subway” display “$1.50 and if they enter “walk” display “Free”. B. Write a java program that creates the following two arrays: String[] candidates = {“S Jones”,”Justin Fairfax”,”Clark Duncan”}; int[] votes = {7345,4324,3211}; Write the code that will search the votes array for the candidate with the largest number of votes and prints the name...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT