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 8237547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:30:24+00:00 2026-06-07T19:30:24+00:00

What I would like the final code to execute is read a string of

  • 0

What I would like the final code to execute is read a string of names in a text document named, ‘names.txt’. Then tell the program to calculate how many names there are in that file and display the amount of names. The code I have so far was meant to display the sum of the numbers in a text file, but it was close enough to the program I need now that I think I may be able to rework it to gather the amount of strings/names and display that instead of the sum.

Here is the code so far:

def main():
    #initialize an accumulator.
    total = 0.0

    try:
        # Open the file.
        myfile = open('names.txt', 'r')

        # Read and display the file's contents.
        for line in myfile:
            amount = float(line)
            total += amount

        # Close the file.
        myfile.close()

    except IOError:
        print('An error occured trying to read the file.')

    except ValueError:
        print('Non-numeric data found in the file.')

    except:
        print('An error occured.')

# Call the main function.
main()

I am still really new to Python programming so please don’t be too harsh on me. If anyone can figure out how to rework this to display the amount of numbers/names instead of the sum of numbers. I would greatly appreciate it. If this program cannot be reworked, I would be happy to settle for a new solution.

Edit: This it an example of what the ‘names.txt’ will look like:

john

mary

paul

ann

  • 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-07T19:30:25+00:00Added an answer on June 7, 2026 at 7:30 pm

    If you just want to count the lines in the file

    # Open the file.
    myfile = open('names.txt', 'r')
    
    #Count the lines in the file
    totalLines = len(myfile.readlines()):
    
    # Close the file.
    myfile.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a form and would like the code to execute only if the
I would like to get the final SQL query, with parameter values bound (replaced)
Would like someone to take a look at my script and tell me where
Would like to know the c# code to actually retrieve the IP type: Static
First, I would like to have an easy code that creates a cookie called
I have a line of code like this: final Foo foo = (Foo)session.get( Foo.class,
So basically I have these snippets of code and would like them to produce
I have code that I would like to convert so that I am grabbing
I would like to scan a large piece of text using PHP and find
I would like to implement system of callbacks which looks like this (pseudo code):

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.