csv 为什么我的登录系统不工作- Python?

gz5pxeao  于 2023-03-21  发布在  Python
关注(0)|答案(2)|浏览(282)

我正在为我的一个学校项目设计一个登录系统,我已经将代码链接到一个CSV文件,当用户注册时,它会保存他们的用户名和密码。然而,当你试图重新登录时,结果是用户名或密码不正确,但当我说我没有帐户并创建一个帐户时,它就工作了。

import csv
import random

# create a list to store the usernames and passwords of both players
players = []

# loop through each player
for i in range(2):

    # ask if the player has made a username and password before
    newUser = input(f"Player {i+1}, have you made a username and password before?").strip()

    # if the player is new, ask for their name and password and add them to the list
    if newUser == 'no':
        newName = input(f"Player {i+1}, what is your first name?").strip()
        newPassword = input(f"Player {i+1}, what would you like your password to be?").strip()

        players.append([newName, newPassword])

        file = open('ScoreSheet.csv', 'a', newline='')
        writer = csv.writer(file)

        writer.writerow([newName, newPassword])

        file.close()

    # if the player has logged in before, ask for their name and password and check if they match
    elif newUser == 'yes':
        verifyName = input(f"Player {i+1}, what is your name?").strip()
        verifyPassword = input(f"Player {i+1}, what is your password?").strip()

        # loop through each player's login information and check if there is a match
        match_found = False
        for player in players:
            if player == [verifyName, verifyPassword]:
                print(f"Player {i+1}, you are logged in")
                match_found = True
                break

        # if there is no match, inform the player and ask them to try again
        if not match_found:
            print(f"Player {i+1}, wrong username or password. Please try again.")

# print welcome message with the players' names
if len(players) >= 2:
    print(f"Welcome, {players[0][0]} and {players[1][0]}!")
else:
    print("Error: not enough players logged in.")

当我打印CSV文件及其所包含的内容时,会显示以前注册的所有名称:

'finley'  'fin'  '10'
janet janet123 None
james james123 None
charlie charlie123 None
amy amy123 None
fred fred123 None
summer summer123 None
nat nat123 None
nathan nathan123 None
elle elle123 None
charles charles123 None
juno juno123 None
sam sam123 None
samuel samuel123 None
Tom Tom123 None
Thomas Thomas123 None
charlie charlie123 None
grace grace123 None
natalie natalie123 None
Dan Dan123 None
Daniel Daniel123 None

我是新的CSV,所以我用谷歌表创建文件,但后来它得到了在VS代码中打开,只显示2个名称:

finley,fin,10
janet,janet123

格式如下:name,password,score我也使用PyScripter来编写实际的代码

2nbm6dog

2nbm6dog1#

当我阅读您的代码时,您似乎并没有真正阅读.CSV文件,而只是从Players列表中读取。我会尝试在检查登录凭据之前添加此代码。它使用òpen函数读取.CSV文件,然后我们将该数据存储在players列表中

#open the CSV file and read the data into the players list
with open('ScoreSheet.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        players.append(row)
nxagd54h

nxagd54h2#

在您的代码中,您将把新用户的信息附加到球员列表中,但是您没有从CSV文件中阅读数据来检查现有用户。您可以尝试以下代码。
在这个更新的版本中,球员列表是使用csv.reader函数用CSV文件中的数据初始化的。当添加新用户时,他们的信息会使用csv.writer函数附加到球员列表和CSV文件中。
当现有用户登录时,系统将搜索玩家列表以查找与其名称和密码匹配的项。如果找到匹配项,则该用户登录。如果没有,则提示他们重试。

import csv
import random

# create a list to store the usernames and passwords of both players
players = []

# read existing users from the CSV file
with open('ScoreSheet.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        players.append(row)

# loop through each player
for i in range(2):

    # ask if the player has made a username and password before
    newUser = input(f"Player {i+1}, have you made a username and password before?").strip()

    # if the player is new, ask for their name and password and add them to the list and CSV file
    if newUser == 'no':
        newName = input(f"Player {i+1}, what is your first name?").strip()
        newPassword = input(f"Player {i+1}, what would you like your password to be?").strip()

        players.append([newName, newPassword])

        with open('ScoreSheet.csv', 'a', newline='') as file:
            writer = csv.writer(file)
            writer.writerow([newName, newPassword])

    # if the player has logged in before, ask for their name and password and check if they match
    elif newUser == 'yes':
        verifyName = input(f"Player {i+1}, what is your name?").strip()
        verifyPassword = input(f"Player {i+1}, what is your password?").strip()

        # loop through each player's login information and check if there is a match
        match_found = False
        for player in players:
            if player == [verifyName, verifyPassword]:
                print(f"Player {i+1}, you are logged in")
                match_found = True
                break

        # if there is no match, inform the player and ask them to try again
        if not match_found:
            print(f"Player {i+1}, wrong username or password. Please try again.")

# print welcome message with the players' names
if len(players) >= 2:
    print(f"Welcome, {players[0][0]} and {players[1][0]}!")
else:
    print("Error: not enough players logged in.")

'

相关问题