此代码不使用错误处理即可正常运行。有没有更有效的方式来编写此代码?我很想向大家学习。
from getpass import getpass
username = input("Username: ")
grant_access = False
while not grant_access:
password = getpass("Password: ")
if password == 'pass123':
grant_access = True
elif password != 'pass123':
while not grant_access:
password = getpass("Re-enter Password: ")
if password == 'pass123':
grant_access = True
elif password != 'pass123':
continue
print("Logging in...")
答案 0 :(得分:1)
您可以这样操作:
from getpass import getpass
username = input("Username: ")
password = getpass("Password: ")
if password != 'pass123':
while True:
password = getpass("Re-enter Password: ")
if password == 'pass123':
break
print("Logging in...")
甚至是这样:
from getpass import getpass
username = input("Username: ")
password = getpass("Password: ")
while password != 'pass123':
password = getpass("Re-enter Password: ")
print("Logging in...")