[英]i am having trouble with open() command in python
I am currently developing a desktop login-register app for my practice, and was having some trouble with login and register.我目前正在为我的实践开发桌面登录注册应用程序,并且在登录和注册时遇到了一些问题。 If I type
REGISTER
and add my info in f.write()
command it stores my info but after that everything just goes, like the whole file gets formatted (this was register issue).如果我输入
REGISTER
并在f.write()
命令中添加我的信息,它会存储我的信息,但之后一切都会进行,就像整个文件被格式化一样(这是注册问题)。
The login issue is if I want to check whether a name or password in file exists or not (this command could be wrong).登录问题是如果我想检查文件中的名称或密码是否存在(此命令可能是错误的)。 I tried to use
if login_email and login password in f:
but it says that login_email
and password
do not exist.我尝试
if login_email and login password in f:
使用if login_email and login password in f:
但它说login_email
和password
不存在。
Code:代码:
f = open('pass.txt', 'w')
fr = open('pass.txt', 'r')
from time import sleep
login_list = "LOGIN"
register_list = "REGISTER"
if 1 > -3232:
print("Type register for new account\ntype login for login into existing account")
bi = input("==> ")
if bi.upper() in login_list:
print("you are registered?? nice now loginnn!!")
login_1 = input("your username: ")
login_2 = input("your password: ")
if login_1 and login_2 in fr:
print("Nice my program worked??")
exit()
else:
exit()
elif bi.upper() in register_list:
print("you are in register section: ")
sleep(.9)
print("NOTE: Your password should only contain alphabets!")
sleep(4)
reg_1 = input("your username: ")
sleep(.9)
reg_2 = input("your password: ")
sleep(.9)
reg_2v1 = input("confirm password")
if reg_2 == reg_2v1:
f.write(reg_1 + " : " + reg_2 + "\n")
print("now login again,\")
else:
print("invalid password, try again")
else:
print("you gave me the wrong command")
else:
exit()
with
to just open the file around the code that needs to use it.with
来打开需要使用它的代码周围的文件。if login_1 and login_2 in fr:
is not the correct way to test if both the username and password are in the file. if login_1 and login_2 in fr:
不是测试用户名和密码是否都在文件中的正确方法。 Due to operator precedence, that's parsed as if login_1 and (login_2 in fr):
.if login_1 and (login_2 in fr):
。 This just checks that login_1
is not empty, and then just checks if login_2
is in the file.login_1
不为空,然后只检查login_2
是否在文件中。 The second test will never work, because the lines of the file all end with newline, but login_2
doesn't, so they'll never match.login_2
以换行符结尾,但login_2
不会,所以它们永远不会匹配。 You need to check for the fully formatted line, including the newline.您需要检查完全格式化的行,包括换行符。
if f'{login_1} : {login_2}\n' in fr:
if bi.upper() in login_list:
seems suspicious. if bi.upper() in login_list:
似乎可疑。 login_list
is not a list, it's a string. login_list
不是一个列表,它是一个字符串。 So this will check whether bi.upper()
is any substring -- it will succeed if the user enters log
or in
or gi
, not just login
.bi.upper()
是否是任何子字符串——如果用户输入log
或in
或gi
,而不仅仅是login
,它将成功。 Is that intentional? Full code:完整代码:
from time import sleep
login_list = "LOGIN"
register_list = "REGISTER"
if 1 > -3232:
print("Type register for new account\ntype login for login into existing account")
bi = input("==> ")
if bi.upper() in login_list:
print("you are registered?? nice now loginnn!!")
login_1 = input("your username: ")
login_2 = input("your password: ")
with open('pass.txt', 'r') as fr:
if f'{login_1} : {login_2}\n' in fr:
print("Nice my program worked??")
exit()
else:
exit()
elif bi.upper() in register_list:
print("you are in register section: ")
sleep(.9)
print("NOTE: Your password should only contain alphabets!")
sleep(4)
reg_1 = input("your username: ")
sleep(.9)
reg_2 = input("your password: ")
sleep(.9)
reg_2v1 = input("confirm password")
if reg_2 == reg_2v1:
with open('pass.txt', 'a') as f:
f.write(reg_1 + " : " + reg_2 + "\n")
print("now login again,")
else:
print("invalid password, try again")
else:
print("you gave me the wrong command")
else:
exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.