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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Introduction

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

 

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education