我正在学习Python,当我试图建立一个身份验证器(登录)时,我遇到了这个错误。
credentials = {
"Peter" : "123",
"Chris" : "Ga1",
"Michael" : "uwu",
"Steve" : "xdx10"
}
entered_username = None
entered_password = None
username_errors = 0
password_errors = 0
while True:
entered_username = input("Please enter your username: ")
entered_password = input("Please enter your password: ")
try:
if entered_password != credentials[entered_username]:
password_errors += 1
if password_errors == 5:
print("Your account has been locked for security reasons.")
exit()
print("Wrong username or password, try again.")
continue
else:
print("success 1")
except:
username_errors += 1
if password_errors + username_errors == 5:
print("Please try again at a later time.")
print("Wrong username or password, try again.")
continue
print("Welcome {}! Your password is {}.".format(entered_username, entered_password))
break
所以 exit()
中,在代码的一半左右,在 if
块状 if
块的 try
块。
运行代码,五次输入正确的密码失败,应该是让它打印 "你的账户因安全原因已被锁定。"然后退出代码。但是,它却做出了这样的回溯。
如果我把 exit()
,回溯消失了。
我不知道是不是因为这个 try
except
, 但我一直试图解决这个问题,像一个吓人的小时,我死在里面 (ಠ_ಠ).
EDIT : 没有粘贴追溯 (facepalm)
Your account has been locked for security reasons.
Wrong username or password, try again.
Please enter your username: Traceback (most recent call last):
File "C:/Users/myname/PycharmProjects/stem1401python/py200603/login_form_v3/stem1401_python_login_form_v3.py", line 42, in <module>
entered_username = input("Please enter your username: ")
ValueError: I/O operation on closed file.
我再补充一下这个。我不知道为什么在exit()之后还能打印出一些东西来. 比如就在 print("Your account has been locked for security reasons.")
有 exit()
Your account has been locked for security reasons.
Wrong username or password, try again.
Please enter your username: Traceback (most recent call last):
File "C:/Users/myname/PycharmProjects/stem1401python/py200603/login_form_v3/stem1401_python_login_form_v3.py", line 42, in <module>
entered_username = input("Please enter your username: ")
ValueError: I/O operation on closed file.
上述错误是由于 pycharm ide
到了 exit()
文件中的一行,它只是 closes the file not terminates the running process
所以当它到达continue的时候,它又回到了循环,当它试图获取输入()的时候,它 throws a traceback that the IO ops can't be done in closed files
所以这就是问题所在,如果你用闲置运行文件,它将成功运行,没有回溯,因为。it will automatically kill the running process when file is exited
,或者如果你想在pycharm中运行代码,只需使用 os.abort()
而不是 exit()
因为它将中止进程
credentials = {
"Peter" : "123",
"Chris" : "Ga1",
"Michael" : "uwu",
"Steve" : "xdx10"
}
entered_username = None
entered_password = None
username_errors = 0
password_errors = 0
import os
while True:
entered_username = input("Please enter your username: ")
entered_password = input("Please enter your password: ")
try:
if entered_password != credentials[entered_username]:
password_errors += 1
if password_errors == 5:
print("Your account has been locked for security reasons.")
os.abort()
print("Wrong username or password, try again.")
continue
else:
print("success 1")
except:
username_errors += 1
if password_errors + username_errors == 5:
print("Please try again at a later time.")
print("Wrong username or password, try again.")
continue
print("Welcome {}! Your password is {}.".format(entered_username, entered_password))
break