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

  • Home
  • SEARCH
  • 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 7637873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:02:17+00:00 2026-05-31T08:02:17+00:00

Another question. This program counts and numbers every line in the code unless it

  • 0

Another question.

This program counts and numbers every line in the code unless it has a hash tag or if the line is empty. I got it to number every line besides the hash tags. How can I stop it from counting empty lines?

  def main():

    file_Name = input('Enter file you would like to open: ')

    infile = open(file_Name, 'r')

    contents = infile.readlines()
    line_Number = 0
    for line in contents:
        if '#' in line:
            print(line)
            if line == '' or line == '\n':
                print(line)
        else:
            line_Number += 1 
            print(line_Number, line)

    infile.close()

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-05-31T08:02:18+00:00Added an answer on May 31, 2026 at 8:02 am

    You check if line == '' or line == '\n' inside the if clause for '#' in line, where it has no chance to be True.
    Basically, you need the if line == '' or line == '\n': line shifted to the left 🙂

    Also, you can combine the two cases, since you perform the same actions:

    if '#' in line or not line or line == '\n':
        print line
    

    But actually, why would you need printing empty stings or '\n'?

    Edit:
    If other cases such as line == '\t' should be treated the same way, it’s the best to use Tim’s advice and do: if '#' in line or not line.strip().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I asked this question in another way , and got some interesting responses but
I am making a Winodws Phone 7 program, I've got another question. So, my
This question is based on another question of mine (thankfully answered). So if in
This question is related to another question which I asked yesterday! List all links
This question is kind of related to another question but I have a specific
This question is related to another question I asked Basically, I have 2 horizontally
This question is related to another question I just posted . I'm prepping for
Another question related to this one . I have a List<SortableObjects> that is the
So I saw an answer in another question saying that this should work: using
I asked this question in another post - How do I put a <div>

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.