Python - base64 - comparing user input vs stored PIN
<p>I am having problems when checking if the inputted pin(check_pin) == correct pin (pin)</p>

<p>the test outputs of the check_pin and pin are the same, but it does not see them as equivalent (line 4)        </p>

<p>code: </p>

<pre><code>pin_check = input("Please input your 4 digit PIN number: ")
print("g"+str(base64.b64encode(bytes(pin_check, "utf-8"))))
print(pin)
    if "g"+str(base64.b64encode(bytes(pin_check, "utf-8"))) == pin:
        y = 0
    else:
        v = v + 1
        y = int(y) - 1
        print("Incorrect PIN,",y,"attempts remaining.\n")
</code></pre>

<p>Outputs:</p>

<pre><code>Please input your 4 digit PIN number: 1234 [user input, correct pin[1234]]
gb'MTIzNA=='    [stored pin]
gb'MTIzNA=='    [user input pin]

Incorrect PIN, 2 attempts remaining.  [it should set y = 0, not print this line]
</code></pre>

<p>Stored pin: Pin.txt consists of several lines of: gb'MTIzNA=='</p>

<pre><code>    import linecache
    fo = open("Pin.txt", "r")
    pin = linecache.getline('Pin.txt', int(card_no))
    print(pin)
    fo.close()
</code></pre>

<p>Given how you read from the file, notice that <code>linecache.getline</code> will also read the newline character at the end of the line. So the content of <code>pin</code> is actually <code>"gb'MTIzNA=='\n"</code> and that's not equal to the base64-encoded value for <code>pin_check</code>.</p>

<p>The fact that in your sample output you have an extra newline between the third and fifth line shows just that (as the second line, judging from the code, is actually the encoded user input pin).</p>

<p>What you might want to do is</p>

<pre><code>pin = linecache.getline('Pin.txt', int(card_no)).strip()
</code></pre>

<p>that is, strip the line you just read.</p>

python base64