user1649077
user1649077

Reputation: 115

Python strings aren't equal

Sorry if the answer to this question may be obvious, but I'm very new to Python (just first started reading a small document about the differing structure and other things from C this morning). While practicing, I decided to make an ATM. However, something weird in the verification process happened, where it compares the input password to the password in a .txt file representing a user database. Despite the two strings are perfectly equal (and yes, I've checked the type, both are class str), my script is completely failing to compare the two correctly! I'm looking and I'm sure I'm missing something obvious, but I just can't find it.

Here's the relevant bits:

class MockUserInterface:
    def __init__(self):
        ccn = input('Enter your Credit Card Number --> ')
        password = input('Enter the corresponding password --> ')
        self.db = MockDatabase()
        self.processUser(ccn, password)

processUser(self, ccn, password) passes ccn and password to VerifyUser to get a False|dictionary value...

class MockDatabase:
    def __init__(self):
        self.initdata = open('D:/users.txt', 'r')
        self.data = {}
        line = 0
        for user in self.initdata:
            line += 1
            splitted_line = user.split(',')
            self.data[splitted_line[0]] = {'Name' : splitted_line[1], 'Password' : splitted_line[2], 'Balance' : splitted_line[3], 'id' : line}
        self.initdata.close()

    def verifyUser(self, ccn, password):
        if ccn in self.data:
            if ccn == self.data[ccn]['Password']:
                return self.data[ccn]
            else:
                print(password, self.data[ccn]['Password'])
        else:
            print(self.data)

The users.txt looks like this:

13376669999,Jack Farenheight,sh11gl3myd1ggl3d4ggl3,90001
10419949001,Sardin Morkin,5h1s1s2w31rd,90102
12345678900,Johnathan Paul,w3ll0fh1sm4j3sty,91235
85423472912,Jacob Shlomi,s3ndm35h3b11m8,-431
59283247532,Anon Tony,r34lp0l1t1k,-9999

After running the script, the output is:

C:\Python33\python.exe D:/PythonProjects/ATM(caspomat).py
Enter your Credit Card Number --> 13376669999
Enter the corresponding password --> sh11gl3myd1ggl3d4ggl3
sh11gl3myd1ggl3d4ggl3 sh11gl3myd1ggl3d4ggl3

Process finished with exit code 0

Again, sorry if the answer is obvious or I'm not giving enough info!

Upvotes: 2

Views: 437

Answers (1)

planestepper
planestepper

Reputation: 3297

You are comparing ccn to the password - not the password arg with the user's stored password...

if ccn == self.data[ccn]['Password']:

should be

if password == self.data[ccn]['Password']:

Upvotes: 9

Related Questions