Ask Question
9 May, 03:21

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.

+4
Answers (1)
  1. 9 May, 03:36
    0
    The code is given below with necessary comments for understanding

    Explanation:

    #Open the input file.

    infile = open ("numbers. txt", "r")

    #Declare and initialize the required variable sum

    #to store the sum of integers read from the file.

    sum = 0

    #Declare and intialize a variable to store the current

    #number read from the file in the integer form.

    curNum = 0

    #Traverse the input file using for loop.

    for curLine in infile:

    #Assign the number read from the file to the

    #variable curNum in the integr form.

    curNum = int (curLine)

    #Add the numbers read from the file to the

    #variable sum to get the sum of all the

    #numbers read from the file.

    sum + = curNum

    # Display the value of the variable runsum.

    print (sum)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “A file named numbers. txt contains an unknown number of lines, each consisting of a single integer. Write some code that computes the sum ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers