【发布时间】:2023-03-21 10:02:01
【问题描述】:
我正在使用 Python 3.6.3。
我正在尝试验证 csv 文件中的用户名和密码。用户名和随后的密码在文本文件的新行上,所以当附加到一个我称为“up”的空数组时,它变成了一个二维数组,每一行都是“up”中的一个列表。我要求用户输入用户名和密码。于是我尝试使用for循环(for x in up)通过up[up.index(x)]遍历up中的每个列表,然后使用.index(username) 之后直接在一个名为 j 的变量中 (j = up[up.index(x)].index(username))。将给出代码,突出显示我遇到问题的部分。 这些是 csv 文件的内容:Text File
当我运行代码时,它会返回一个错误,提示 用户名不在列表中。我已经搜索了答案,但找不到任何东西。有什么我忽略的吗?
任何帮助将不胜感激。
import csv
validoption = ["i","u"]
while True:
option = input("Sign in or sign up\nPlease enter 'i' to sign in or 'u' to sign up: ")
if option in validoption:
if option is "i":
with open("login.txt","r") as l:
up = []
read = csv.reader(l)
count = 0
for line in read:
up.append(line)
count=+1
invalid = True
while invalid:
username = input("Please enter your username: ")
password = input("Please enter your password: ")
if [[y is username for y in x] for x in up]:
for x in up:
j = up[up.index(x)].index(username)
if password in up[up.index(x)][j+1]:
invalid = False
else:
print("Password is incorrect")
else:
print("Username is not recognised")
else:
with open("login.txt","a") as l:
username = input("Please enter your username: ")
while True:
password = input("Please enter your password\nPlease make sure that your password is longer than 8 characters, has a capiatal letter and a number: ")
if len(password)<8:
if any(p.isupper() for p in password):
if any(p.isdigit() for p in password):
break
else:
print("Password must have one number in it\n")
else:
print("Password must have one capital letter\n")
else:
print("Password must have more than 8 charaters")
userdetails = username+","+password
l.write(userdetails)
l.write("\n")
谢谢你:)
【问题讨论】:
标签: python-3.6