Kristina D.

asked • 11/10/20

Python Files Assignment Help

I need help creating a code that meets the following requirements:

  1. Prompt the user to enter a file name/path to read
  2. Use open(filename) to open and read the file specified by the user (in the variable, filename). Read each line of the file and interpret each line as follows:
  3. Ignore blank lines
  4. If the line begins with the text name followed by a space, then add the remained of the line's text to a list (initially empty) of names
  5. If the ling begins with the text food followed by a space, then add the remained of the line's text to a list (initially empty) of foods
  6. After processing the full contents of the file (every line) print each name from the names list along with a randomly selected item from the foods list. No two names should be printed with the same food. (Hint: each time you select a random food, remove it from the foods list so that it cannot possibly be selected again)
  7. For each line printed in the requirement above (a name with a random, unique food) also write the same line out to a file named output.txt. Remember that the print function automatically starts a new line, but the write function of a file does not, so you'll need to include /n


2 Answers By Expert Tutors

By:

Patrick B. answered • 11/13/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.