卡牌游戏登录系统异常:无法正确读取CSV并展示登录用户名
登录系统故障:始终显示CSV文件前两个用户的欢迎信息
我为一款卡牌游戏开发的登录系统曾运行正常,目前出现异常:无论重新登录已有玩家,还是注册新玩家,系统始终返回Welcome 'finley' and janet!的提示信息——这两个是CSV文件中的前两个用户名,并非当前登录的用户。
使用的代码(开发工具:PyScripter,CSV编辑工具:VSC)
import csv import random import keyboard # create a list to store the usernames and passwords of both players players = [] key = True # read existing users from the CSV file with open('ScoreSheet.csv', 'r') as file: reader = csv.reader(file) next(reader) 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) >= 0: print(f"Welcome, {players[0][0]} and {players[1][0]}!") access = True else: print("Error: not enough players logged in.")
系统返回结果示例
Player 1, have you made a username and password before? yes Player 1, what is your name? janet Player 1, what is your password? janet123 Player 1, you are logged in Player 2, have you made a username and password before? yes Player 2, what is your name? juno Player 2, what is your password? juno123 Player 2, you are logged in Welcome, 'finley' and janet!
测试情况:逻辑与匹配功能正常
我已对系统的逻辑和匹配能力进行测试,结果显示匹配功能正常:
测试代码
with open('ScoreSheet.csv', 'r') as f: reader = csv.reader(f, delimiter=',') for row in reader: print(f"CSV row: {row}") if row == [verifyName, verifyPassword]: print(f"You are logged in with username: {verifyName} and password: {verifyPassword}") match_found = True break print(f"Input username: {verifyName}") print(f"Input password: {verifyPassword}") with open('ScoreSheet.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print(row['name'], row['password'], row['score']) with open('ScoreSheet.csv', 'r') as f: reader = csv.reader(f, delimiter=',') for row in reader: print(type(row)) # add this line to check the data type if row == [verifyName, verifyPassword]: print('You are logged in') match_found = True break with open('ScoreSheet.csv', 'r') as f: reader = csv.reader(f, delimiter=',') for row in reader: print(row) # add this line to check the rows if row == [verifyName, verifyPassword]: print('You are logged in') match_found = True break else: print(f"Row {row} does not match {verifyName} and {verifyPassword}")
测试返回结果
CSV row: ['name', 'password', 'score'] CSV row: ["'finley'", " 'fin'", " '10'"] CSV row: ['janet', 'janet123'] CSV row: ['james', 'james123'] CSV row: ['charlie', 'charlie123'] CSV row: ['amy', 'amy123'] CSV row: ['fred', 'fred123'] CSV row: ['summer', 'summer123'] CSV row: ['nat', 'nat123'] CSV row: ['nathan', 'nathan123'] CSV row: ['elle', 'elle123'] CSV row: ['charles', 'charles123'] CSV row: ['juno', 'juno123'] You are logged in with username: juno and password: juno123 Input username: juno Input password: juno123 '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 nathaniel nathaniel123 None lisa lisa123 None joel joel123 None jake jake123 None jane jane123 None james james123 None <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'> You are logged in ['name', 'password', 'score'] Row ['name', 'password', 'score'] does not match juno and juno123 ["'finley'", " 'fin'", " '10'"] Row ["'finley'", " 'fin'", " '10'"] does not match juno and juno123 ['janet', 'janet123'] Row ['janet', 'janet123'] does not match juno and juno123 ['james', 'james123'] Row ['james', 'james123'] does not match juno and juno123 ['charlie', 'charlie123'] Row ['charlie', 'charlie123'] does not match juno and juno123 ['amy', 'amy123'] Row ['amy', 'amy123'] does not match juno and juno123 ['fred', 'fred123'] Row ['fred', 'fred123'] does not match juno and juno123 ['summer', 'summer123'] Row ['summer', 'summer123'] does not match juno and juno123 ['nat', 'nat123'] Row ['nat', 'nat123'] does not match juno and juno123 ['nathan', 'nathan123'] Row ['nathan', 'nathan123'] does not match juno and juno123 ['elle', 'elle123'] Row ['elle', 'elle123'] does not match juno and juno123 ['charles', 'charles123'] Row ['charles', 'charles123'] does not match juno and juno123 ['juno', 'juno123'] You are logged in Here is your card 'finley': Yellow 5 Here is your card janet: Yellow 1
内容的提问来源于stack exchange,提问作者fcorby
相关产品推荐
相关产品推荐

