Python-3.6 - "Not In List" 错误
Python-3.6 - "Not In List" Error
我正在使用 Python 3.6.3.
我正在尝试从 csv 文件验证用户名和密码。用户名和后续密码位于文本文件中的新行中,因此当附加到我称为 "up" 的空数组时,它变成了一个二维数组,每一行都是 "up" 中的一个列表。我要求用户提供用户名和密码。因此,我尝试使用 for 循环(for x in up)通过 up[up.index(x)][ 遍历 up 中的每个列表,然后直接在名为 j 的变量中使用 .index(username) (j = up[up.index(x)]。索引(用户名))。将给出代码,并突出显示我遇到问题的部分。
这些是 csv 文件的内容:Text File
当我 运行 代码时 returns 出现一个错误,指出 用户名不在列表中。 我已经搜索了答案,但找不到任何东西。有什么我忽略的吗?
任何帮助将不胜感激。
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")
谢谢:)
y is username
测试两个值是否是 相同的 对象,而不是等效对象。请改用 ==
。
此外,您应该考虑使用用户名作为 dict
的键,然后您可以使用 in
并消除应用程序代码中的一些循环。
我正在使用 Python 3.6.3.
我正在尝试从 csv 文件验证用户名和密码。用户名和后续密码位于文本文件中的新行中,因此当附加到我称为 "up" 的空数组时,它变成了一个二维数组,每一行都是 "up" 中的一个列表。我要求用户提供用户名和密码。因此,我尝试使用 for 循环(for x in up)通过 up[up.index(x)][ 遍历 up 中的每个列表,然后直接在名为 j 的变量中使用 .index(username) (j = up[up.index(x)]。索引(用户名))。将给出代码,并突出显示我遇到问题的部分。 这些是 csv 文件的内容:Text File
当我 运行 代码时 returns 出现一个错误,指出 用户名不在列表中。 我已经搜索了答案,但找不到任何东西。有什么我忽略的吗?
任何帮助将不胜感激。
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")
谢谢:)
y is username
测试两个值是否是 相同的 对象,而不是等效对象。请改用 ==
。
此外,您应该考虑使用用户名作为 dict
的键,然后您可以使用 in
并消除应用程序代码中的一些循环。