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..

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter7: File Handling And Applications
Section: Chapter Questions
Problem 15RQ
icon
Related questions
Question

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
Transcribed Image Text: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
er.py
friends - Notepad
# K File Edit Format View Help
# dDenny
24
#og Penny
fil
28
|Lenny
acd
20
cou
Jenny
whi 24
Transcribed Image Text:er.py friends - Notepad # K File Edit Format View Help # dDenny 24 #og Penny fil 28 |Lenny acd 20 cou Jenny whi 24
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT