So below is the question of the problem for my class. I have attached a picture of my python program and I keep getting than error message. I have also attached the list of the friends and age. using python Now write a program named filereader.py that reads and displays the data in friends.txt. This program should also determine and print the average age of the friends on file. That will require an accumulator and a counter. Use a while loop to process the file, print the data, and modify the accumulator and counter. Then close the file and display the average age accurate to one decimal place. See the Sample Output..
Please help,
So below is the question of the problem for my class. I have attached a picture of my python program and I keep getting than error message. I have also attached the list of the friends and age.
using python
Now write a program named filereader.py that reads and displays the data in friends.txt. This program should also determine and print the average age of the friends on file. That will require an accumulator and a counter. Use a while loop to process the file, print the data, and modify the accumulator and counter. Then close the file and display the average age accurate to one decimal place. See the Sample Output..
SAMPLE OUTPUT
My friend Denny is 24
My friend Penny is 28
My friend Lenny is 20
My friend Jenny is 24
Average age of friends is 24.0
I appreciate the help
Kevin
List index out of range happens when you try to access the elements that are outside the range of list
In friends.txt file, one line is having name and the next line is having the respective age
If you try to split each line, you will get only one word in list and this will be at index 0 and if you try to access at index 1, it results in the error
So, to fix this you can keep track of count of the line and based on that we have to decide whether it is name or age
Step by step
Solved in 3 steps with 1 images