[英]Program keeps displaying None even after input
The code is meant to be a simple login code that saves the login information to a .txt file and then when logging in reads the text file to check the user details.该代码是一个简单的登录代码,它将登录信息保存到 .txt 文件中,然后在登录时读取文本文件以检查用户详细信息。
The code runs up until I create an account or try to login and put in my username and password then it comes back with None.代码一直运行,直到我创建一个帐户或尝试登录并输入我的用户名和密码,然后它返回 None。 I don't understand why it's coming back with None
我不明白为什么它会返回 None
def AskAccount():
account = input("\nDo you have an account setup
already? (Y/N)\n")
if account == "Y":
loginexisting()
elif account == "N":
createacc()
else:
print("please type Y or N")
AskAccount()
def loginexisting():
print("Your account already exists, please login\n")
username = input("Please enter your username:")
password = input("Please enter your password:")
f = open('accounts.txt', 'r')
info = f.read()
info = info.split()
if username in info:
index= info.index(username) +1
usr_password = info[index]
if usr_password == password:
return "Welcome Back," + username
else:
return "password entered is wrong"
else:
print("Username is not correct")
print(createacc())
def createacc():
print("Lets create an account for you\n")
username = input("Please input your username:\n")
password = input("please input your password\n")
f = open("accounts.txt",'r')
info = f.read()
if username in info:
return "Name Unavailable. Please Try Again"
f.close()
f = open("accounts.txt",'w')
info = info + " " + username + " " + password
f.write(info)
f.close()
print("Your account details have been saved\n")
print("please login\n")
print(AskAccount())
At the end of your file, you print(AskAccount())
.在文件末尾,您
print(AskAccount())
。 This prints the return value of the function, but AskAccount
does not have a return statement, thus it returns None
.这将打印函数的返回值,但
AskAccount
没有返回语句,因此它返回None
。 If you want it to print your desired output, you will need to add return statements.如果您希望它打印所需的输出,则需要添加 return 语句。
def AskAccount():
account = input("\nDo you have an account setup
already? (Y/N)\n")
if account == "Y":
return loginexisting()
elif account == "N":
return createacc()
else:
print("please type Y or N")
return AskAccount()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.