所以我的代码如下:
它的工作方式是接受外部列表文件中的用户输入,但是我希望它接受来自用户的输入,然后检查该输入是否在外部文件中的任何位置。目前,它仅接受第一行作为第一用户,然后接受其他用户。我希望它接受输入,即使它不是代码的第一行。
with open("users.txt") as f:
lines = set("users.txt")
x = input("First Player, please enter a valid username and password (In the form: Username , Password): ")
for lines in f:
if (x) in ("users.txt"):
lines2 = [line.rstrip("\n") for line in open("users.txt")]
y = input("Second Player, please enter a valid username and password (In the form: Username , Password): ")
for lines in f:
if (y) in ("users.txt"):
我尝试了简单的代码,直到更复杂的代码,例如:
if (y in lines for y in lines)
答案 0 :(得分:0)
您正在使用文件名而不是文件内容。
with open("users.txt") as f:
allowed_names = set(line.strip() for line in f)
x = input("First player name, password")
p1_name, p1_password = x.split(",")
y = input("Second player name, password")
p2_name, p2_password = y.split(",")
if p1_name in allowed_names:
...
if p2_name in allowed_names:
...