Input Validation loop / read file txt with Python
<p>My first issue is my file is not being read by the script and truthfully I do not know why.</p>

<p>Second, the issue is my loop "y" or "Y" is not looping the question of account input for a user, it just continues to repeat until I hit "n" or "N" to exit....which works. (so I guess my loop works properly in that sense)</p>

<p>My current situation
I have a txt file that I need to read account numbers from.
I need it to loop and continue to ask for validation of account numbers even if the account number is wrong.</p>

<p>I would like the output to look like this</p>

<p>------------My needed output----------------</p>

<pre><code>&gt;&gt;&gt;
Enter the account number to be validated: 456321
Account number 456321 is not valid.
Enter another card number? Enter Y or N: Y
Enter the account number to be validated: 5552012
Account number 5552012 is valid.
Enter another card number? Enter Y or N: N
&gt;&gt;&gt;
</code></pre>

<p>-----------End Needed output--------------</p>

<p>-----------My output at the moment---------</p>

<pre><code>&gt;&gt;&gt; 
============ RESTART: G:\Software Design\accounts.py ============
Enter the account number to be validated: 4453221
Account number 4453221 is not valid
Enter another card number? Enter Y or N: y
Enter another card number? Enter Y or N: n
&gt;&gt;&gt; 
</code></pre>

<p>----------End current output-----------</p>

<p>--------My Code----------</p>

<pre><code>def main():

    #setting loop control
    another = 'y'

    try:
        # open file READ ONLY charge_accounts.txt
        infile = open('charge_accounts.txt', 'r')

        # Setting accountNum variable
        accountNum = int(input('Enter the account number to be validated: '))

        if accountNum in infile:
            accountNum = int(infile.readline(accountNum))
            print('Account number ' + str(accountNum) + ' is valid')
        else:
            print('Account number ' + str(accountNum) + ' is not valid')
            # Loop controls for other account inputs
            while another == 'y' or another == 'Y':
                # Get another account
                another = input('Enter another card number? ' + 'Enter Y or N: ')
                if another == 'n' or another == 'N':
                    break

        infile.close()

    # Extra credit +5 points ( catching errors )
    except IOError:
        print('An error occured trying to read')
        print('the file', charge_accounts.txt)
main()
</code></pre>

<p>With regards to your loop, everything you want to be repeated needs to be inside of it. So think about what you've written, <code>while another == y:</code> the <em>body of the loop</em> will be repeated. So, my advice would be to think about <em>where you start looping</em>. </p>

<p>Also, <code>accountNum in infile:</code> will always be false since you converted <code>accountNumb</code> to an <code>int</code>, and it needs to stay a string. Here is another head's up: </p>

<pre><code>if accountNum in infile:
    accountNum = int(infile.readline(accountNum))
    print('Account number ' + str(accountNum) + ' is valid')
</code></pre>

<p>Will not work like you think it will anyway. When you check if something is in a <code>file</code> object, it moves the stream position to the end. This means when you <code>readline</code> it will return an empty string! If you think about it, you don't really need the <code>readline</code> anyway.</p>

python loops readfile