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
![4 #opened friends.txt file
5 file = open("friends.txt","r")
6 accumulator = 0
7 counter = 0
8 while True:
for x in file:
1st = x.split()
if(len(1st)!=0):
name = 1st[0]
10
11
12
age = int(1st[1])
accumulator += age
13
14
15
counter += 1
16
print("My friend", name, "is", age)
17
print("\nAverage age of my friends is",round (accumulator/counter,1))
18
break
19 file.close()
|
Shell x
Python 3.7.9 (bundled)
>>> %Run filereader.py
Traceback (most recent call last):
File "C:\Users\Kevin\Desktop\School\Intro Computer Programming\Chapter 6\Bonus Chpt 6\
filereader.py", line 13, in <module>
age = int (1st[1])
IndexError: list index out of range](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fca4b40f7-fa18-4cbf-a6b3-216b47710386%2Fc40faebe-fbff-4bd5-b006-f34828e8bd73%2Fwqtnamm_processed.png&w=3840&q=75)
![er.py
friends - Notepad
# K File Edit Format View Help
# dDenny
24
#og Penny
fil
28
|Lenny
acd
20
cou
Jenny
whi 24](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fca4b40f7-fa18-4cbf-a6b3-216b47710386%2Fc40faebe-fbff-4bd5-b006-f34828e8bd73%2Feg72c7o_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)