1. Write a program in python with no imports named filemaker.py that will be used to store the first name and age of some friends in a text file named friends.txt. The program must use a while loop that prompts the user to enter the first name and age of each friend. Each of these entries should be written to its own line in the text file (2 lines of data per friend). The while loop should repeat until the user presses Enter (Return on a Mac) for the name. Then, the file should be closed and a message should be displayed. See Sample Output.SAMPLE OUTPUTEnter first name of friend or Enter to quit DennyEnter age (integer) of this friend 24Enter first name of friend or Enter to quit PennyEnter age (integer) of this friend 28Enter first name of friend or Enter to quit LennyEnter age (integer) of this friend 20Enter first name of friend or Enter to quit JennyEnter age (integer) of this friend 24Enter first name of friend or Enter to quitFile was created2. 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 OUTPUTMy friend Denny is 24My friend Penny is 28My friend Lenny is 20My friend Jenny is 24Average age of friends is 24.0 ANSWER #2

Question
Asked Dec 1, 2019
97 views

1. Write a program in python with no imports named filemaker.py that will be used to store the first name and age of some friends in a text file named friends.txt. The program must use a while loop that prompts the user to enter the first name and age of each friend. Each of these entries should be written to its own line in the text file (2 lines of data per friend). The while loop should repeat until the user presses Enter (Return on a Mac) for the name. Then, the file should be closed and a message should be displayed. See Sample Output.
SAMPLE OUTPUT

Enter first name of friend or Enter to quit Denny
Enter age (integer) of this friend 24
Enter first name of friend or Enter to quit Penny
Enter age (integer) of this friend 28
Enter first name of friend or Enter to quit Lenny
Enter age (integer) of this friend 20
Enter first name of friend or Enter to quit Jenny
Enter age (integer) of this friend 24
Enter first name of friend or Enter to quit
File was created

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

 

ANSWER #2

check_circle

Expert Answer

Step 1

The name of the program is filereader.py

Open the file named friends.txt

Use a variable named accumulator to do the sum of the friend’s age.

Use a variable named counter to count the friends.

In while loop

Read the data from the file named friends.txt

Do the sum of the friend's age.

Count the total friends.

Display the data from the file friends.txt

Calculate the average age of friends and display.

Close the file.

Step 2

Data in friends.txt file.

py
friends.txt
Denny 24
Penny 28
Lenny 20
Jenny 24
help_outline

Image Transcriptionclose

py friends.txt Denny 24 Penny 28 Lenny 20 Jenny 24

fullscreen
Step 3

The python program is:

#The name of the program is filereader.py

 

#open the file

file = open("friends.txt")

 

#accumulator is used to sum the age of the friends

accumulator = 0.0

#counter is used to count the total number of friends

counter = 0

 

#while loop to read the data from the file till the file is empty

while True:

   

    #read data from file

    line = file.readline()

   

    #when all data is read then display rhe Average age of friends.

    if (""==line):

        #calculate the average age of friends

        avgAge = accumulator/counter

        #display the average age

        print("Average age of friends is %.1f"%avgAge)

   &...

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
help_outline

Image Transcriptionclose

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

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Python

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Question in Image. I was wondering why they are not equal even thought their contents are the same

A: Code:

question_answer

Q: Given the assumptions above and the page table below, translate the following virtual addresses (exp...

A: Assuming the page size to be 4KB. The calculations for physical addresses are given below: (a) VA = ...

question_answer

Q: Compiler returning errors with calculations and declarations. Screenshot of File.txt included.  ...

A: The code is working fine as I have tested it and the Output is attached at the end.Code://importing ...

question_answer

Q: Implement the following two methods in javascript:   String multiply2(String num) - return num*2 wit...

A: Program Instructions:In the function multiply2(), take one digit at a time, multiply it by two, and ...

question_answer

Q: The prompt asks: Write a function that calculates the amount of money a person would earn over a per...

A: Program Approach-1)Define function named totalEarned () with years as a parameter.2)Declare and init...

question_answer

Q: Explain why web application security is vital. What could happen to an insecure application? Why is ...

A: Web application securityWeb application security testing ensures that the information system is capa...

question_answer

Q: Write a recursive function rec_string that produces the output shown below for the corresponding fun...

A: Program Algorithm-STARTStep1: Define the rec_string method.Step2: Compare the length of string to ze...

question_answer

Q: Write a program in python with no imports named filemaker.py that will be used to store the first na...

A: Create a file named filemaker.pyPrompt user for name of a friend and his age repeatedly until user e...

question_answer

Q: Exercise 1 Consider a hash table of size 13, and assume open addressing with linear probing   Show ...

A:  As the given size of the table is 13, as well as the number of elements to be inserted in the table...