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
Urgent help<br> Write a program that prints ‘Hello World’ to the screen
lapo4ka [179]
Class newprog
 { 
      public static void main()
       {

                 System.out.println("Hello world");
              }
}
4 0
3 years ago
What will be a fundamental aspect of future games??
alina1380 [7]
I believe that the answer to the question provided above is that with the age of improving technology, the aspects of future games will be based more unto electronic games or e-games.
Hope my answer would be a great help for you.    If you have more questions feel free to ask here at Brainly.
7 0
3 years ago
Read 2 more answers
Which of the following is true about main content? Select all that apply.
lions [1.4K]

Answer:

TRUE - Main Content should be created with time, effort, and expertise, and should not be copied from another source.

True -High quality Main Content (MC) allows the page to achieve its purpose well.

Explanation:

Web design and development is the creation of web pages.web pages can be static or links to multiple pages. A static web page has only one page and a site address. Multiple web pages, are series of related web pages linked together with a link tag line of code.

In a web page, a main content is the main information needed to be passed across by the programmer or blogger, it varies from page to page in a multiple web site. It must be unique and contain the main information of the web page.

7 0
3 years ago
Read 2 more answers
_________________ uses soap or detergent to physically remove germs, dirt, and impurities from surfaces or objects.
ra1l [238]

Answer:

humans,washing mashines,dish washers

Explanation:

8 0
2 years ago
What is the function of the kernel of an operating system? It is an application that allows the initial configuration of a Cisco
Phoenix [80]

Answer:The kernel provisions hardware resources to meet software requirements.

Explanation: Kernel is the program which behaves as the central component of the operating system.The main purpose of the kernel is to control the operation of the system to manage it.

Monolithic kernel and micro-kernel are two types of kernel units present in the computer system where they work for fulfilling the software requirement by attaching it to the hardware parts.

Other option are incorrect because it does not work for Cisco device, does not perform any specific feature only , no linkage is created between the hardware and the electronics nor reacts as navigation bar.

3 0
3 years ago
Other questions:
  • 3. If B3=6 and D5=8, what would the following function return? IF(B3&gt;D5, "Closed", D5-B3) *
    10·2 answers
  • what is the restaurant with the black pom tree and yellow backround three letter name from hi guess the restaurant
    14·2 answers
  • Într-o curte sunt G găini și O oi. Să se determine numărul de capete și numărul de picioare din curte.
    13·1 answer
  • What is indentation?
    8·2 answers
  • Consider the following partial class definitions: public class A1 { public int x; private int y; protected int z; ... } public c
    12·1 answer
  • HELP PLS
    5·2 answers
  • 17. What are the basic modes of operation of 8255?Write the features of mode 0 in 8255?
    8·1 answer
  • Life without internet points
    7·2 answers
  • 100 POINTS!!!!!!!
    15·2 answers
  • Instant Search can NOT be used to search through archived messages.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!