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
Thepotemich [5.8K]
2 years ago
8

I am having trouble with this python question. I am in a beginner level class also so any solution that isn't too complex would

be great so I can wrap my head around it! Thanks!
Given a text file containing the availability of food items, write a program that reads the information from the text file and outputs the available food items. The program first reads the name of the text file from the user. The program then reads the text file, stores the information into four separate lists, and outputs the available food items in the following format: name (category) -- description

Assume the text file contains the category, name, description, and availability of at least one food item, separated by a tab character ('\t').

Ex: If the input of the program is:

food.txt
and the contents of food.txt are:

Sandwiches Ham sandwich Classic ham sandwich Available
Sandwiches Chicken salad sandwich Chicken salad sandwich Not available
Sandwiches Cheeseburger Classic cheeseburger Not available
Salads Caesar salad Chunks of romaine heart lettuce dressed with lemon juice Available
Salads Asian salad Mixed greens with ginger dressing, sprinkled with sesame Not available
Beverages Water 16oz bottled water Available
Beverages Coca-Cola 16oz Coca-Cola Not available
Mexican food Chicken tacos Grilled chicken breast in freshly made tortillas Not available
Mexican food Beef tacos Ground beef in freshly made tortillas Available
Vegetarian Avocado sandwich Sliced avocado with fruity spread Not available
the output of the program is:

Ham sandwich (Sandwiches) -- Classic ham sandwich
Caesar salad (Salads) -- Chunks of romaine heart lettuce dressed with lemon juice
Water (Beverages) -- 16oz bottled water
Beef tacos (Mexican food) -- Ground beef in freshly made tortillas

Computers and Technology
1 answer:
Delicious77 [7]2 years ago
5 0

The code:

filename = input("> ")

with open(filename, "r") as file:

   items = file.read()

   items = items.split("\n")

   for index, item in enumerate(items):

       items[index] = item.split("\t")

   for item in items:

       if item[3] == "Available":

           print(f"{item[1]} ({item[0]}) -- {item[2]}")

Explenation:

1. We make the user enter the file's path, in our case "food.txt". This does only work if the .txt file is in the <u>same</u> directory as our code.

2. We get the content of our .txt file with the "open" function. If we use the "open" function inside of a "with" statement we don't have to deal with closing the file after we opened it. The file will close as soon as we exit the statement. The argument "r" indicates that we only want to read the file, not do anything else with it.

3. However the variable "file" doesn't contain the <u>contents</u> of "food.txt", only the class object of our file (if you try "print(file)" you'll only get nonsense). We can simply get the content of the file by calling ".read()" after.

4. We can separate this long str using the ".split()" function. The split function seperates a string by another sertain string (in our case "\n", since the different items are seperated by a newline). Now "items" is a list of strings.

5. Next we go through all items in "items" and separate them by "\t", again using the ".split()" function. Enumerate makes a list of indexes and values from another list (in our case, "items").

6. Now we have a list of lists of strings. The only thing left to do is go through every item and print it if the item's third item is equal to "Available" (notice that this is case-sensative). And print out the result in the desired format.

The code works for me, please ask me if there is anything you don't understand (or if the code is incorrect). Happy to help :D

You might be interested in
What is the difference between auto fill and fill handle ?​
lidiya [134]

Answer:

  • Auto fill a software function that automatically completes data (like the data that has been entered previously) without the user needing to type it in full.

  • The Fill Handle is a feature in Excel that fills the data automatically with a specific pattern in your spreadsheet cell.

hope u liked the answer :)

7 0
2 years ago
____ contain instructions for the OS for hardware devices, such as the keyboard, mouse, and video card, and are stored in the sy
AnnyKZ [126]

The Motherboard contains instructions for the OS for hardware devices, such as the keyboard, mouse, and video card, and are stored in the systemroot\Windows\System32\Drivers folder.

Learned this in middle school, your welcome ;)

3 0
3 years ago
Students can use eNotes to type notes directly on screen and
zalisa [80]
A. review or print them later
6 0
3 years ago
Read 2 more answers
Topological sort requires only a tiny addition to the DFS algorithms. Exactly one of the following data structures is required t
Alekssandra [29.7K]

Answer:

Stack.

Explanation:

Topological sort requires a bit of addition to DFS algorithm.So DFS can be done by two ways that are either you can use recursion or you can use Stack data structure to implement DFS.

Since recursion uses stack memory when it makes recursive calls and if you want to do it iteratively you can stack data structure.

8 0
2 years ago
Why might location be important when searching for a job?
Elan Coil [88]
D I think because none of the others seem like very important things, 
8 0
2 years ago
Read 2 more answers
Other questions:
  • How i can connect to internet automatically when i switch on my computer?
    8·1 answer
  • Trish has bought a new computer that she plans to start working on after a week. Since Trish has not used computers in the past,
    10·1 answer
  • The ____ algorithm was the first public key encryption algorithm developed (in 1977 and published for commercial use.
    8·1 answer
  • Exploring Arraylists. Remember that arraylists are good for adding and removing things, and that they are clock-cycle friendlier
    13·1 answer
  • Help!!!!!!!!!!!!!!!!!
    11·1 answer
  • Write a C++ program to find if a given array of integers is sorted in a descending order. The program should print "SORTED" if t
    14·1 answer
  • Public static double secret(int first, double second)
    6·1 answer
  • Gabriel's sister called him about a message that suddenly appeared on her screen that says her software license has expired and
    11·1 answer
  • Please what do you guys think about this ?
    10·2 answers
  • What does CPU stand for
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!