Question

In: Computer Science

DO NOT ANSWER THIS[1. Write a program named filemaker.py that will be used to store the...

DO NOT ANSWER THIS[1. Write a program 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] DO NOT ANSWER THIS

Code for the above program

fp = open("friends.txt", "w")

firstname = input("Enter first name of friend or Enter to quit ")

while firstname!="":

age = int(input("Enter age (integer) of this friend "))

print(firstname, file=fp)

print(age, file=fp)

firstname = input("Enter first name of friend or Enter to quit ")

fp.close()

print("File was Created")

the QUESTION IN 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

Solutions

Expert Solution

// text code for filereader.py

#open file in read mode
fp = open("friends.txt", "r")

# initialize accumulator and counter to 0
accumulator = 0
counter = 0

#is name tracks whether we are scanning Name or age in file
is_name = True

# name will store the last name from file
name = ""

# for each line in file
for line in fp:
  
# if line is name
if(is_name == True):
# make is_name false, means we are reading age
is_name = False
#rstrip('\n') removes the new line at the end of line
name = line.rstrip('\n')
  
  
else:
# make is_name false
is_name = True
# increment counter
counter = counter + 1
# add the age
accumulator = accumulator + int(line.rstrip('\n'))
#print the name and age
print("My friend", name, "is", line.rstrip('\n'))
  

#close the file
fp.close()


# print the avearge if counter is not zero
if(counter != 0):
print("Average age of friends is", round((accumulator/counter), 1))

else: print("There is no friend in the list")

# SCREENSHOTS

# filereader.py

# friends.txt file

# OUTPUT


Related Solutions

Write a program named filemaker.py in python that will be used to store the first name...
Write a program named filemaker.py in python 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...
Write program to store ten integers in an array and the program will do the following...
Write program to store ten integers in an array and the program will do the following Find the average of even marks Find how many prime integers inserted Note: please solve it by c , not c++
write a script named compute.sh that is used to do simple arithmetic for the user. There...
write a script named compute.sh that is used to do simple arithmetic for the user. There should be no command line arguments. Instead, all values from the user should be prompted for and read in to the script using the read command. Specifically, you need to ask the user for two integers and an operation string. The operation should be "add", "sub", "mul", "div", or "exp", for addition, subtraction, multiplication, division, and exponentiation, respectively. Your script should take the two...
Write a program named "cleanDocument.pl" that will do the following: Reads two parameters from the command...
Write a program named "cleanDocument.pl" that will do the following: Reads two parameters from the command line (@ARGV) The first parameter is the name of a file and the second parameter is a word If no command line parameters are present, it asks the user to input the values Opens the text file that is passed as the first command line argument Loops through the lines of the text file and replaces all occurrence of the word (the second command...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
In the previous assessment, you used a static set of named variables to store the data...
In the previous assessment, you used a static set of named variables to store the data that was entered in a form to be output in a message. For this assessment, you will use the invitation.html file that you modified in the previous assessment to create a more effective process for entering information and creating invitations for volunteers. Rather than having to enter each volunteer and create an invitation one at a time, you will create a script that will...
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
1, Write an appropriate compile-time initialization for a memory location named courseNumber. Store CS 158 as...
1, Write an appropriate compile-time initialization for a memory location named courseNumber. Store CS 158 as the initial value in the memory location. ______________________________ 2, To declare a memory location to store the town where you were born, you would write _______________________.  The identifier (memory location name) you should use is homeTown. It follows the rules for naming identifiers. Be sure to end your declaration with a semicolon. 3, The default data type used for floating point values is __________. You...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT