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
Question 4 / 5
kkurt [141]

Answer:

it can damage you liver. and you can drink to much and bleed inside.

4 0
2 years ago
Definition of laptop
zaharov [31]
A device that sucks bc mien broke
7 0
2 years ago
Read 2 more answers
A manufacturing company has several one-off legacy information systems that cannot be migrated to a newer OS due to software com
Digiron [165]

The resiliency technique which would provide the aforementioned capabilities is: D. Full backups.

An operating system (OS) can be defined as a system software which is pre-installed on a computing device, so as to manage or control software application, computer hardware and user processes.

In this scenario, a manufacturing company cannot migrate its several one-off legacy information systems (IS) to a newer operating system (OS), due to software compatibility issues.

Resiliency can be defined as a measure of the ability of a network, server, storage system, computing system or data center, to recover quickly and continue operating when it experience adverse conditions such as:

  • Power failure.
  • Equipment failure.
  • Data loss.

In Computers and Technology, there are four (4) main resiliency technique and these include:

I. Redundancy.

II. RAID 1+5.

III. Virtual machines.

IV. Full backups.

Full backup is a resiliency technique which create backups of the systems for recovery and it allows operating system (OS) patches to be installed on computer systems.

Read more: brainly.com/question/17586013

3 0
2 years ago
Which of these is a way that a doctor can examine a patient?
SIZIF [17.4K]

Answer:

D. all the above

Explanation:

because doctor uses stethoscope to listen patients heart, he also used hammer to check patients reflexes and also doctor has to listen to the patient.

5 0
3 years ago
Assume the following variable definition appears in a program:
AnnZ [28]

Answer:

cout << setprecision(2)<< fixed << number;

Explanation:

The above statement returns 12.35 as output

Though, the statement can be split to multiple statements; but the question requires the use of a cout statement.

The statement starts by setting precision to 2 using setprecision(2)

This is immediately followed by the fixed manipulator;

The essence of the fixed manipulator is to ensure that the number returns 2 digits after the decimal point;

Using only setprecision(2) in the cout statement will on return the 2 digits (12) before the decimal point.

The fixed manipulator is then followed by the variable to be printed.

See code snippet below

<em>#include <iostream>  </em>

<em>#include <iomanip> </em>

<em>using namespace std;  </em>

<em>int main()  </em>

<em>{  </em>

<em> // Initializing the double value</em>

<em> double number = 12.3456;  </em>

<em> //Print  result</em>

<em> cout << setprecision(2)<< fixed << number;  </em>

<em> return 0;  </em>

<em>}  </em>

<em />

4 0
3 years ago
Other questions:
  • An icon in a document preset that looks like the play button on a YT video, what type of document is the preset meant to be used
    13·2 answers
  • If you want to store the information that a user types in response to the input() function, what do you need to do? (select the
    11·1 answer
  • You have no control over who views your social network information
    13·2 answers
  • What is the purpose of the overload keyword in the ip nat inside source list 1 pool nat_pool overload command?
    8·1 answer
  • What are two examples of ways an electronic record may be distributed to others?
    15·1 answer
  • How to write greater than or equal to in excel if function
    7·1 answer
  • A folder has been shared with other users and set to read-only. What does this mean for users?
    12·2 answers
  • In which of the following scenarios would you choose to embed versus import data?
    8·1 answer
  • The image below shows an encoding for a black and white pixel image. The first two
    13·1 answer
  • In many supermarkets customers can pay for their shopping using credit cards. (a) Name two items of information stored on the ma
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!