简体   繁体   中英

How do I compare strings of inputs from a text file?

I want to compare userN to nameDb and userP to passDb and if both of the strings matches or does not matches, loginResult would changed to 1 on 0.(Where 1 being correct, 0 being false)

[Edit1]: Changed initial loginResult to 1 from 0, added 2 print statement at the if else under login function

[Edit2]: It seems like there might be a problem with the way I'm comparing strings in my login function. Even if both of the strings matches, it still goes to the else statement. Resulting in loginResult to be always 0.

loginResult = 1

userN = input("Username:")
userP = input("Password:")

def login(userN,userP):
    global = loginResult
    nameDb = open("files1", mode ="r")
    passDb = open("files2", mode ="r")

    print("Textfile username:",nameDb.readline())
    print("Textfile password:",passDb.readline())


    if(userN == nameDb.readline() and userP == passDb.readline()):
        loginResult = 1
        print("Ding")
        return
    else:
        loginResult = 0
        print("Dong")

login(userN, userP)

if(loginResult == 1):
    print("Welcome !")
elif(loginResult == 0):*emphasized text*
    print("Error !")

Output:

Username:Kelvin12

Password:Maxi12

Textfile username: Kelvin12

Textfile password: Maxi12

dong

Error !

Process returned 0 (0x0) execution time : 4.688 s Press any key to continue . . .

First of all always use open with with , then trim and replace newlines from your strings:

loginResult = 0

userN = input("Username:")
userP = input("Password:")

def login(userN,userP):
    with open("files1", mode ="r") as nameDb, open("files2", mode ="r") as passDb:
        if(userN == nameDb.readline().strip().replace('\n', '') and userP == passDb.readline().strip().replace('\n', '')):
            loginResult = 1
            return
        else:
            loginResult = 0


login(userN, userP)

if(loginResult == 1):
    print("Welcome !")
elif(loginResult == 0):
    print("Error !")

You are trying to assign different value to the global variable loginResult from within your function. If you want to do that you have to use global keyword. You can add just one line to your original code:

loginResult = 1

userN = input("Username:")
userP = input("Password:")

def login(userN,userP):

    global loginResult  # now loginResult is global, not local

    nameDb = open("files1", mode ="r")
    passDb = open("files2", mode ="r")

    print("Textfile username:",nameDb.readline())
    print("Textfile password:",passDb.readline())


    if(userN == nameDb.readline() and userP == passDb.readline()):
        loginResult = 1
        print("Ding")
        return
    else:
        loginResult = 0
        print("Dong")

login(userN, userP)

if(loginResult == 1):
    print("Welcome !")
elif(loginResult == 0):*emphasized text*
    print("Error !")

Can you print what type of data nameDb and passDb are? If it is a list, use 'in' not '=='.

Using for loop it prints out all the content in the in nameList as X variable. And if x matches userN 's strings it will then execute below code.

userN = input("Username:")
userP = input("Password:")

with open("file1", mode ="r") as nameDb:
    nameList = nameDb.read().splitlines()

with open("file2", mode ="r") as passDb:
    passList = passDb.read().splitlines() 
#using .read.splitlines removes "\n" in the output

for x in nameList:

    if x == userN:

        loginName = 1

        break
    else:
        loginName = 0


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM