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
David was editing his audio recording in his audio editing software. He adjusted the vocal track of his audio toward
Dmitrij [34]

Answer:

b

Explanation:

balancing the sound

a                h    o

l                 e    u

a                      n

n                      d

c

i

n

g

6 0
2 years ago
When a customer makes a request or complaint, the goal of customer service is to
Brrunno [24]

Solution:

When a customer makes a request or complaint, the goal of customer service is to show the customer how to remain calm and courteous.

Because the work of the costumer care is  to serve as customers' first point of contact for complaints, questions, requests, feedback, or any other needs; these professionals are essentially charged with managing relationships between clients and the company.

Thus the required answer is D

7 0
3 years ago
A ____ in Excel is like a notebook.
laila [671]
A workbook in Excel is like a notebook.
3 0
3 years ago
The purpose of the ________ protocol is to ensure the integrity of the communication.
Vladimir [108]

The purpose of the transmission control protocol is to ensure the integrity of the communication.

The TCP/IP network architecture also refers to the Internet architecture. The Transmission Control Protocol (TCP) is a transport layer protocol, and the Internet Protocol (IP) is a network layer protocol.

<h3>What is Transmission control protocol ?</h3>

Transmission Control Protocol, or TCP, is a communications standard that enables computer hardware and software to exchange messages over a network. It is intended to send packets over the internet and make sure that data and messages are successfully delivered through networks.

  • TCP's (Transmission Control Protocol) purpose is to regulate data transport so that it is dependable. Connection management, dependability, flow control, and congestion control are the four fundamental TCP functions. Connection initialization (a three-way handshake) and termination are included in connection management.

Learn more about Transmission control protocol here:

brainly.com/question/14280351

#SPJ4

3 0
2 years ago
Everyone holds some stereotypical attitudes.
lozanna [386]
The answer to your question is True.

Everyone holds some type of stereotypical attitudes, whether it's judging someone based on what they wear or how they look everyone has them. 

8 0
4 years ago
Read 2 more answers
Other questions:
  • Where is the insert function button found in microsoft excel?
    11·1 answer
  • Impact of computer on education
    6·2 answers
  • Which of the preventive strategies is described below: Violence often escalates. Behaviors like excessive complaing, hostility,
    10·1 answer
  • How many bits must be “flipped” (i.e., changed from 0 to 1 or from 1 to 0) in order to capitalize a lowercase ‘a’ that’s represe
    11·1 answer
  • What are the five types of alignment in Word?
    12·2 answers
  • Suppose 8 people want to communicate with each other using public key encryption. The communication between any pair of them is
    7·1 answer
  • Your baby brother has found the hammer and is eagerly eyeing one of the boxes. Describe and analyze an algorithm to determine if
    15·1 answer
  • The pay of an hourly worker is calculated by multiplying the hours worked by the hourly rate—up to 40 hours; any hours worked be
    11·1 answer
  • NAT is able to stop ________. Group of answer choices a) scanning probes sniffers from learning anything about the internal IP a
    8·2 answers
  • Write a program that asks the user to provide a word, validate that it is a word, and print the word.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!