Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:10:48+00:00 2026-06-15T09:10:48+00:00

I am trying to make a program which grabs a list of numbers from

  • 0

I am trying to make a program which grabs a list of numbers from a file (which could change in lines and size), and then print out the total of all the numbers and the average. I had no problems doing this when I had a set number of linereads, but am confused on the ‘proper’ way when the lineread changes every run.

This is my work-in-progress code. I read around a bit and found the correct (?) way of looping through the file to find the length, but not sure how to implement it since it throws some type of IO error currently. Thanks for the help!

def main():
    filename = input("Enter file name (name.txt):")
    try:
        file = open(filename, "r")
    except IOError:
        print("Error opening file!")

    totalLines = totalLineGet(filename)

    results = []
    for x in range(totalLines):
            results.append(getLineNumber(x+1, file))

    print("Total = ", numTotal)
    print("Average = ", numAvg)

def totalLineGet(_filename):
    count = 0
    _file = open(_filename, "r")
    for x in open(_file):
        count+= 1
    return count

def getLineNumber(linetoget, _file):
    try:
        intNumber = int(number = _file.readline())
    except ValueError:
        print("Error in file data!")

    return intNumber

main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:10:49+00:00Added an answer on June 15, 2026 at 9:10 am

    I’m not sure what you want to do… but you should be able to get the answer in one pass.
    You can use enumerate() to number an iterable object, in this case a file, if you need to know the item/line number count.

    Assuming a single int() per line:

    with open(filename, "r") as in_f:
        numbers = []
        for line in in_f: 
            line = line.strip() # remove whitespace
            if line: # make sure there is something there
                number_on_line = int(line) 
                numbers.append(number_on_line)
        sum_of_numbers = sum(numbers)
        avg_of_numbers = sum(numbers)/len(numbers)
    

    if this is CSV data you should look into using the csv module, it will split the line into rows/columns for you.

    import csv
    filename = "somefile"
    with open(filename, "rb") as in_f: # <-- notice "rb" is used
        reader = csv.reader(in_f)
        for row in reader:
            for col in row:
                # do stuff
                ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a program that can take an .mp3 file from
I'm trying to make a program that creates and reads a binary file, which
I'm trying to make a stopwatch program, which will write czas to file.txt .
I'm trying to make a program, which is basically a shopping list. The purpose
I'm trying to make a program which will create a sum of directories the
I'm trying to make a program which creates a spaceship and I'm using the
Hello guys look to my code I'm trying to make a program which asks
Context: I'm trying to do is to make a program which would take text
Im trying to make a program that retrieves an endless amount of numbers that
I'm trying to make a program that, using pointers, detects lines in an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.