Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The following question pertains to Python. Consider the following Python bar chart script below. Modify the bar chart script to read data from a file
The following question pertains to Python.
Consider the following Python bar chart script below.
Modify the bar chart script to read data from a file called "names.txt" by doing the following:
- Modify the Python script by adding code that reads in the names and ages file into two arrays: "names" and "ages."
- Erase the fruits and counts arrays from the original script.
- Pass the names and ages arrays to the figure() and vbar() functions instead of the fruits and counts arrays.
- In the figure() function, change the title of the chart from Fruit Counts to Person Ages.
Notice that modifying the python script to read the names and ages is by adding lines of code to perform these steps:
- Open the names.txt file in reading mode.
- Call the readline() function to read the header line and ignore it.
- Create two empty arrays, names=[] and ages = []
- Create a for loop that scans the file line by line. For each line:
- Split it into name and age using the split() function and the \t tab delimiter.
- Add the name to the names array and the age to the ages array using the append() function.
- Close the names.txt file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started