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...
Java Using NetBean Write a program to define an integer variable named as roomNumber. Assign 408...
Java Using NetBean Write a program to define an integer variable named as roomNumber. Assign 408 to the variable. Then display a message in System.out as “The programming class is in room ”+roomNumber.
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 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...
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)
Python Programming Build a python programming that asks the user for a three-letter substring. The program...
Python Programming Build a python programming that asks the user for a three-letter substring. The program should then proceed to read the file strings.txt. Each line in strings.txt will contain a string. Your program should then test to see how many non-overlapping occurrences of the three-letter substring occur in that string and test whether the string is a palindrome. The program should then proceed to create an output file string_stats.txt, which contains the original data on one line and the...
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.
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...