我试图在Repl.it中完成Python代码的登录过程。它将搜索名为“UDfile”的csv文件以确认用户名和密码,这样它就可以访问我正在创建的纸牌游戏。以下是文件的外观:
markd,12345
pltwo,67890
forhb,10\/3
但是,我为2个用户创建了这个。只要我输入的用户名高于第二个人的用户名,第一个人登录就可以了。但是当我尝试输入第二个用户名时,不管是否正确,它总是说它无法识别该用户名。我怀疑它没有进行完整的搜索。我怎样才能让它彻底搜索这个csv文件,以便它允许我输入P2的密码,从而继续程序
我在stackoverflow中搜索了一些类似于“当它说现有用户名不存在时如何让这段代码找到它?”的东西,但是我找不到任何东西
我还尝试添加
系统出口
到底
打印(“未找到用户名”)
在user_findtwo(file,user)中,希望它能彻底搜索文件,并在完全找不到文件时终止,但在程序说一次也找不到文件后,代码会立即终止
最后,我试着循环说
while row[0] != usertwo:
print("Username not found")
sys.exit()
我希望它只搜索整个文本文件,并说“用户名未找到”,直到找到播放器2的用户名,但它只是无休止地打印“用户名未找到”
import csv
import sys
def main():
login()
logintwo()
show_menu()
menu_choice = int(input("Welcome to the RYG card game! Press \"1\" and then \"Enter\" to play a game:"))
options(menu_choice)
def login():
with open("UDfile", "r") as file:
file_reader = csv.reader(file)
user_find(file_reader)
file.close
def user_find(file):
user()
for row in file:
if row[0] == user:
print("Username is found:", user)
user_found = [row[0], row[1]]
pass_check(user_found)
break
else:
print("Username not found")
sys.exit()
def user():
global user
user = input("Player 1, enter your username (must be above player 2's desired name):")
def logintwo():
with open("UDfile", "r") as file:
file_reader = csv.reader(file)
user_findtwo(file_reader, user)
file.close
def user_findtwo(file, user):
usertwo = input("Player 2 enter your name:")
if usertwo == user:
print("Usernames must be different. Restart the program.")
sys.exit()
else:
for row in file:
if row[0] == usertwo:
print("Username is found:", usertwo)
user_found = [row[0],row[1]]
pass_checktwo(user_found)
break
else:
print("Username not found")
def pass_check(user_found):
x = True
while x:
user = input("Enter your password:")
if user_found[1] == user:
print("Password match.")
x = False
else:
print("Password doesn't match.")
sys.exit()
def pass_checktwo(user_found):
y = True
while y:
usertwo = input("Player 2 enter your password:")
if user_found[1] == usertwo:
print("Password match.")
y = False
else:
print("Password doesn't match.")
sys.exit()
有关预期和实际结果,请参见背景
你能用这个方法循环行吗?您可以使用len(csvFileArray)来获取长度,让它循环到最大值以避免索引超出范围,并且可以让它在找到需要的内容后通过函数(这样它就不会继续循环?)
https://stackoverflow.com/a/39629619
只是一个快速的想法
相关问题 更多 >
编程相关推荐