Python 为什么我的代码似乎跳过了函数的一半?
我正在创建一个系统,在这个系统中,它会检查用户是否已经声明了用户名,但在创建帐户时,它会询问用户想要的用户名,然后将其发送回选项屏幕,询问用户是否想要登录或创建帐户Python 为什么我的代码似乎跳过了函数的一半?,python,python-3.x,file-handling,Python,Python 3.x,File Handling,我正在创建一个系统,在这个系统中,它会检查用户是否已经声明了用户名,但在创建帐户时,它会询问用户想要的用户名,然后将其发送回选项屏幕,询问用户是否想要登录或创建帐户 def user_create(): f = open("users.txt", "a") unique = 0 name = input("Enter a username: ") with open("users.txt&quo
def user_create():
f = open("users.txt", "a")
unique = 0
name = input("Enter a username: ")
with open("users.txt", "r") as file:
file_read = csv.reader(file)
filerow = 1
for row in file_read:
if row[0] == name:
print("This username is already taken, try another one")
file.close()
unique = False
break
else:
unique = True
filerow = filerow + 1
if unique == True:
file.close()
elif unique == False:
signin1()
else:
pass
if name.replace(" ", "").isalpha():
print("Name is valid")
else:
print ("Name is invalid")
user_create()
password = input("Enter a password: ")
f.write(name+","+password+",\n")
f.close()
print("Account created")
signin1()
输出:
Enter a username: testuser
1. Login
2. Create
Which one would you like to do?
下一步应该询问用户密码,但似乎只跳过了一半的代码
所有登录代码:
def signin1():
print ("1. Login")
print ("2. Create")
choice = input("Which one would you like to do? ")
if choice == "1":
existence = os.path.exists("users.txt")
if existence == False:
print("There are no authorised accounts yet created")
signin1()
else:
mainsignin()
elif choice == "2":
user_create()
else:
print("Invaild choice")
signin1()
def user_create():
f = open("users.txt", "a")
unique = 0
name = input("Enter a username: ")
with open("users.txt", "r") as file:
file_read = csv.reader(file)
filerow = 1
for row in file_read:
if row[0] == name:
print("This username is already taken, try another one")
file.close()
unique = False
break
else:
unique = True
filerow = filerow + 1
if unique == True:
file.close()
elif unique == False:
signin1()
else:
pass
if name.replace(" ", "").isalpha():
print("Name is valid")
else:
print ("Name is invalid")
user_create()
password = input("Enter a password: ")
f.write(name+","+password+",\n")
f.close()
print("Account created")
signin1()
def mainsignin():
f = open("users.txt", "w")
f.close
with open("users.txt", "r") as file:
file_reader = csv.reader(file)
user_find(file_reader)
def user_find(file):
user = input("Enter your username: ")
filerow = 1
login = False
for row in file:
if row[0] == user:
print("Username found:", user)
user_found = [row[0], row[1]]
login = True
break
else:
filerow = filerow + 1
if login == True:
global user1
user1 = user
pass_check(user_found)
else:
print ("Could not find user with the name: "+str(user))
signin1()
def pass_check(user_found):
userpass = input("Enter your password: ")
if user_found[1] == userpass:
print("password match")
str(user_found)
game_login_user2()
else:
print("password does not match")
mainsignin()
您的问题源于此条件:
elif unique==False:
首先将值0
分配到unique
,然后根据文件内容将True
或False
分配到该值中。如果文件为空,则unique
在文件循环的末尾仍将有一个0
。现在,这里潜在的令人惊讶的事情是0==False
被评估为true!这是因为0
是“Falsy”(有关更多信息,请参阅)。因此,当您希望代码移到else时,实际上它返回到signn1
为了避免将来出现类似的错误,我强烈建议使用x is True
和x is False
来代替x==True
和x==False
。我还建议不要在同一个变量中使用不同的类型(int、bool),因为这可能会导致像这样的错误
最后,我建议您学习如何使用调试器。如果您逐行运行此代码,您会很快发现此问题。我不确定是否正确理解您的问题,问题似乎出现在
user\u create
函数中,unique
在该函数的一开始设置为0
(False
),如果users.txt
恰好为空,signin1
将被调用,即使给定了唯一的用户名
def user_create():
f=打开(“users.txt”、“a”)
unique=0#unique设置为False
名称=输入(“输入用户名:”)
打开(“users.txt”、“r”)作为文件:
file_read=csv.reader(文件)
filerow=1
对于文件_read:#中的行,如果users.txt为空,则以下8行将不会运行
如果行[0]==名称:
打印(“此用户名已被使用,请尝试其他用户名”)
file.close()文件
唯一=错误
打破
其他:
唯一=真
filerow=filerow+1
如果unique==True:
file.close()文件
elif unique==False:
signn1()#即使给出了唯一的用户名,这里也会调用signn1
其他:
通过
在我看来,您是在直接登录elif unique==False
,而您可能希望用户收到一条错误消息。当您以附加模式('a'
)打开文件时,它会自动搜索到底。这意味着您无法读取任何内容,因此循环永远不会运行。@BlueStar我不确定这是否是问题所在,因为错误消息位于“if row[0]==name”下,然后关闭文件,然后将unique设置为False,然后中断循环,然后引导到符号的if语句in@Blckknght因此,如果没有创建该文件,它将在函数的开头创建它,以便代码的其余部分可以正常工作。之后在代码中,它会重新打开文件,上面写着“以open(“users.txt”,“r”)作为文件”,这有点奇怪,因为我没有收到错误消息。谢谢,我不太明白当文件为空时会发生什么,但您的注释起到了帮助作用,代码现在又开始工作了