Mixed-up Code QuestionsΒΆ

Create a function write_line(file, output) that writes the contents of the string output to file. Be sure to add a newline character "\n" when you write output to the file.

Write a function write_line(file, output) that writes the contents of the string output to file. Be sure to add a newline character "\n" when you write output to the file. Also make sure that you close the file after writing to it.

Create a function read_n_lines(file, n) that reads the first n lines from file and returns them in a list. Be sure to remove the end of line character from each line before you add it to the list and close the file after you have read the lines.

Write a function read_n_lines(file, n) that reads the first n lines from file and returns them in a list. Be sure to remove the end of line character from each line before you add it to the list and close the file after you have read the lines

Create a function count_words(file) that returns the number of words in the passed file. First create a variable num and initialize it. Then open the file. Loop reading a line from the file. Break the line at spaces and add the length of the resulting list to num. After you have read all the lines, close the file and return num.

Write a function count_words(file) that returns the number of words in the passed file. First create a variable num and initialize it. Then open the file. Loop reading a line from the file. Split the line at spaces and add the length of the resulting list to num. After you have read all the lines, close the file and return num.

Create a function count_starting_with(file, str) that returns the number of lines in the passed file that start with the characters in str. Be sure to close the file.

Data file: dogs.txt
Labrador Retriever
German Shepherd
Golden Retriever
Beagle
Bulldog
Yorkshire Terrier
Boxer
Poodle
Rottweiler
Dachshunds

Create a function count_starting_with(file, str) that returns the number of lines in the passed file that start with the characters in str. Be sure to close the file.

Create a function write_squares(file) which writes the squares of 1 (inclusive) to 10 (inclusive) with each one on a single line to file.

Create a function write_squares(file) which writes the squares of 1 (inclusive) to 10 (inclusive) with each one on a single line to file.

You have attempted of activities on this page