Question

PYTHON population_file = open('populations.txt') # 1. Define a variable called total_population, assign 0 to this variable...

PYTHON

population_file = open('populations.txt')
# 1. Define a variable called total_population, assign 0 to this variable

for line in population_file.readlines():
# 2. Add line to the total_population, convert line to an int first

# 3. Close the population_file using the close() method

total_population_file = open('total_population.txt', 'w')
# 4. Use the write() method to write the total_population to the total_population_file. Convert total_population to a string

# 5. Close the total_population_file using the close() method

Require

1.Program creates a new file called total_population.txt

2.The file total_population.txt contains a single line representing the sum of all populations from the populations.txt file

Below is populations.txt file

49177
56540
80429
91923
53752
390113
49631
133570
192294
50789

Homework Answers

Answer #1

The program is given below: that open populations.txt file, read populations from file convert line to int, add into total_population and write total_population to total_population.txt file.

#open populations file
population_file = open('populations.txt')

# 1. variable called total_population, assign 0 to this variable
total_population=0

for line in population_file.readlines():
# 2. add line to the total_population, convert line to an int first
total_population = total_population + int(line)
  
# 3. Close the population_file using the close() method
population_file.close()

total_population_file = open('total_population.txt', 'w')
# 4. Use the write() method to write the total_population to the total_population_file.
#Convert total_population to a string
total_population_file.write(str(total_population))

# 5. Close the total_population_file using the close() method
total_population_file.close()

The screenshot of code is given below:

populations.txt

49177
56540
80429
91923
53752
390113
49631
133570
192294
50789

(after execution of program ) total_population.txt

1148218

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How would I solve these python problems? A) File Display Download the file from here named...
How would I solve these python problems? A) File Display Download the file from here named numbers.txt . Write a program that displays all of the numbers in the file. numbers.txt contains 22 14 -99 AB B) Error Check Float Input When you want an int input you can check the input using the string isdigit() method. However, there is no comparable check for float. Write a program that asks the user to enter a float and uses a try-except...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features the design of a calculator program using pseudocode and a Python program that uses a list and functions with variable scope to add, subtract, multiply, and divide. Open the M4Lab1ii.py program in IDLE and save it as M4Lab1ii.py with your initials instead of ii. Each lab asks you to write pseudocode that plans the program’s logic before you write the program in Python and...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Write a C++ program to do the following: Declare and assign values to int variables x,...
Write a C++ program to do the following: Declare and assign values to int variables x, y Declare an int variable z; and store the sum of x and y in it Declare a pointer called pz and point it in the heap direction (using new) Store the z value in the heap location using the pz pointer Print the content of the pz pointer Print the pointer (the address)
#Write a function called "write_file" that accepts two #parameters: a filename and some data that will...
#Write a function called "write_file" that accepts two #parameters: a filename and some data that will either #be an integer or a string to write. The function #should open the file and write the data to the file. # #Hints: # # - Don't forget to close the file when you're done! # - If the data isn't a string already, you may need # to convert it, depending on the approach you # choose. # - Remember, this code...
JAVA Write a program that will search a text file of strings representing numbers of type...
JAVA Write a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. Include appropriate error handling in case a line contains more than one int or it contains a String. Wrap all the work you are doing with the file in a try/catch/finally block with appropriate "catch" sections; the finally block will be where you close your file object (if desired, look this...
Write a C Language program called iam.c to: Have a string variable called myName initialized to...
Write a C Language program called iam.c to: Have a string variable called myName initialized to your-name-here.  Print on one line a literal string "Hello " concatenated to myName. In Linux Terminal compile your iam.c source code program to produce a .exe executable. Run your iam.exe and take a print screen image. Upload your iam.exe file here.
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT