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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:03+00:00 2026-05-22T18:41:03+00:00

I’m having a little trouble with converting a text file to a list. the

  • 0

I’m having a little trouble with converting a text file to a list.
the text file is presented like this:

5658845
4520125
7895122
8777541
8451277
1302850
8080152

I have written code that takes user input and tries to determine if the user input is in the list. I am however having some trouble in searching the list as I can only get a result on the last result in the list, where am I going wrong?

def accountReader():

    while True:
        chargeInput = (raw_input ("Enter a charge account to be validated: "))
        if chargeInput == '':
            break
            sys.exit


        else:
            chargeAccount = open('charge_accounts.txt', 'r')
            line = chargeAccount.readline()
            while line != '':
                if chargeInput == line:
                    print chargeInput, 'was found in list.'                    
                else:
                    print chargeInput, 'not found in list.'
                    break           


    chargeFile.close  
  • 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-22T18:41:03+00:00Added an answer on May 22, 2026 at 6:41 pm

    A line-by-line breakdown:

    def accountReader():
    
        while True:
            chargeInput = (raw_input ("Enter a charge account to be validated: "))
            if chargeInput == '':
                break
                sys.exit
    

    Ok, so far so good. You’ve created a loop that repeatedly asks the user for input and breaks when the user inputs nothing.

            else:
                chargeAccount = open('charge_accounts.txt', 'r')
                line = chargeAccount.readline()
    

    Here’s where you start running into problems. readline reads a single line from chargeAccount and stores it in line. That means you can only test one line!

                while line != '':
                    if chargeInput == line:
                        print chargeInput, 'was found in list.'
    

    This further compounds your problem. If chargeInput == line, then this prints a message and then the loop repeats. Since there’s nothing to break out of the loop, this will result in an infinite loop that constantly tests one single line from the file. Also, because each line from the file ends with a newline (\n), chargeInput == line will always yield false (thanks Steven Rumbalski for reminding me of this). Use .strip() (as suggested in matchw’s answer), or, if you can tolerate partial matches, you could use Python’s simple substring matching functionality: if chargeInput in line.

                    else:
                        print chargeInput, 'not found in list.'
                        break           
    
    
        chargeFile.close  
    

    And here, as sarnold pointed out, you’ve misnamed your file; furthermore, it’s in a completely different block of code, which means that you repeatedly open chargeAccount files without closing any of them.

    As you can see from matchw’s post, there is a much simpler way to do what you’re trying to do. But I think you’d do well to figure out how to write this code correctly in the style you’ve chosen. I’ll give you one hint: there should be a line = chargeAccount.readline() inside the innermost while loop. Do you see why? Also, you should probably exit the loop when you succeed in finding a match, not when you fail. Then you should think about a way to test whether the search was a success after the innermost loop has completed.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.