python - 登录总是说不正确
问题描述
我的代码总是说它是不正确的,即使它出现在文本文件中也很困难,它以前可以工作,但现在由于某种原因不能工作。
def select_login_signup():
while True:
selection = input("Welcome to sports.com, please select"
" \"L\" to Log In with your account or \"S\" to create an account: ")
if selection.lower() == 's':
register()
answer = input("Would you like to Log In? Y/N? ")
while not answer:
if answer.lower() == "y":
login()
break
elif answer.lower() == "n":
exit()
else:
answer = False
print("Invalid answer.")
continue
elif selection.lower() == 'l':
login()
break
else:
print("Invalid answer.")
continue
def register():
username = input("Create your username (no more than 10 characters or less than 4.): ")
while 10 < len(username) < 4:
print('username cannot have more than 10 characters or less than 4.')
username = input("Create your username (no more than 10 characters or less than 4.):
")
break
while username.isnumeric():
print("username must contain at least one letter")
username = input("Create your username (no more than 10 characters or less than 4.):
")
break
password = input("Create a password with letters and numbers: ")
while len(password) < 6:
print("Your password must contain more than 6 characters.")
password = input("Create a password with letters and numbers: ")
continue
while password.isnumeric() or password.isalpha():
print("Your password must contain both letters and numbers")
password = input("Create a password with letters and numbers: ")
continue
login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "a")
login_credentials.write(f'\n{username},{password}')
login_credentials.close()
print("Account created successfully.")
def login() -> object:
username = input("Please enter your username: ")
username = username.strip()
password = input("Please enter your password: ")
password = password.strip()
login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "r")
login_credentials.readlines()
with open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', 'r') as
login_credentials:
for line in login_credentials:
login_info = line.split(",")
if username == login_info[0] and password == login_info[1]:
print("Authorized")
authenticated = True
return authenticated
else:
print("Incorrect credentials.")
username = input("Please enter your username: ")
username = username.strip()
password = input("Please enter your password: ")
password = password.strip()
continue
def main():
select_login_signup()
if __name__ == "__main__":
main()
解决方案
使用 open('C:\Users\hmarq\Documents\UsernameAndPassword.txt', 'r') 作为 login_credentials
当你打开一个文件时,你必须格式化数据,因此数据是不一样的。
如果用户名 == login_info[0] 和密码 == login_info[1]:
我希望它对你有用,问候。
推荐阅读
- cucm - 给定内部目录号码,您如何找到指向它的外部号码?
- javascript - 在 node.js 中创建脚本以将种子数据插入 mongodb
- datastax-enterprise - gem -https://github.com/cequel/cequel(它本身使用 https://github.com/datastax/ruby-driver)是否适用于 DSE 6.0?
- java - Hive JDBC 连接问题
- vb.net - 如何以编程方式真正选择datagridview中的一行,而不仅仅是突出显示
- jquery - 如何使导航栏响应?
- sass - 有没有办法通过 SASS 中的类传递参数?
- mysql - http get API 查询云 sql 并以 csv 格式返回
- python-3.x - 无论年份如何,如何按日期的日月部分对数据进行分组?
- java - 使用元数据唯一标识文件