将登录信息与csv数据进行比较并授予访问权限

2024-09-30 20:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序询问用户是否有帐户。如果用户没有帐户,则程序将为用户创建一个新帐户并将其详细信息存储在csv文件中。这部分工作得很好。但如果用户再次运行该程序(在创建帐户后)并尝试使用相同的详细信息(用户名、密码)登录,则无法正常工作。如何将登录信息与csv数据进行比较并授予访问权限?在

##Two Subjects----> Computer Science and History
import csv

def main():
    userAccount()

def userAccount():
    print('-------------------------------')
    userAccount = input('Do you already have an account: ')
    if userAccount == 'No' or userAccount== 'N' or userAccount == 'no' or userAccount == 'n':
        userSName = input('Name : ')
        userSAge = input('Age : ')
        userSYgroup = input('Year Group : ')
        userGname = userSName[:3] + userSAge
        print('Your username is : ',userGname.lower())
        userSword = input('Password: ')
        detail1 = [userSName,userSAge,userSYgroup,userGname,userSword]
        with open('userData.csv','w',newline = '') as csvfile:
            w = csv.writer(csvfile,delimiter = ',')
            w.writerow(detail1)
    elif userAccount == 'Yes' or userAccount == 'Y' or userAccount == 'yes' or userAccount == 'y':
        userLName = input('Username: ')
        passLWord = input('Password: ')

        f = open('userData.csv','r',newline = '')
        for i in f:
            if userLName == f[0,3] and passLWord == f[0,4]:
                print('Log In Successfull')
            else:
                print('Incorrect Username or Password')
                main()      

main()

Tags: orandcsv用户程序inputmain详细信息
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:50

这是一个完全工作的版本,稍作改动。将其添加到原始代码中。虽然它可能会破坏您当前的代码,因为我更改了几个变量的名称。相应地重命名它们。在

##Two Subjects  > Computer Science and History
import csv

def main():
    userAccount()
    print("rest of the code")
    input() # keep window open

def userAccount():
    print('               -')
    # group possible inputs; no need for individual checks
    noAccount  = ["no", "NO", "n", "N"]
    yesAccount = ["yes", "YES", "y", "Y"]
    hasAccount = input('Do you already have an account: ')
    if hasAccount in noAccount:
        userName   = input('Name : ')
        userAge    = input('Age : ')
        userYear   = input('Year Group : ')
        userLogin  = (userName[:3] + userAge).lower()
        print('Your username is : ', userLogin)
        userPW     = input('Password: ')
        details    = [userName, userAge, userYear, userLogin, userPW]
        # open file with 'a' instead of 'w' so multiple records can be added to csv
        with open('userData.csv', 'a', newline='') as csvfile:
            writer = csv.writer(csvfile, delimiter=',')
            writer.writerow(details)
        # once the account is created, return to beginning
        main()
    elif hasAccount in yesAccount:
        userLogin = input('Username: ')
        userPW    = input('Password: ')
        granted   = None
        with open('userData.csv', 'r', newline='') as csvfile:
            reader = csv.reader(csvfile, delimiter=',')
            # loop every row (record) in the csv file
            for row in reader:
                csvLogin = row[3]
                csvPW    = row[4]
                if userLogin == csvLogin and userPW == csvPW:
                    granted = True
                    print('Log In Successfull')
                    # a user is matched; no need to check other records
                    break
        # if input doesn't match any csv record, return to beginning
        if granted is None:
            print('Incorrect Username or Password')
            main()
    else:
        # if user enters something other than yes/no, return to beginning
        main()

main()

相关问题 更多 >