"A file named numbers.txt contains an unknown number of lines, each consisting of a single integer. Write some code that computes the sum of all these integers, and stores this sum in a variable name sum."

Respuesta :

Limosa

Answer:

Following are the program in the Python Programming Language.

#declare variable and initialize to 0

sum = 0

#read the following file

file = open('numbers.txt', 'r')

#set the for loop

for n in file:

  #convert the file string into the integer and perform sum.

  sum += int(n)

Explanation:

The following are the description of the program.

  • Firstly, declare variable 'sum' and initialize the integer type value i.e., 0.
  • Set the variable 'file' that only read the following file text i.e., 'numbers.txt'.
  • Set the for loop that read the content of the file and store in the variable 'n' one by one.
  • Finally, er convert the following string in to the integer and perform the addition.