如果密码正确,我如何发现“已授予访问权限”被打印3次?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果密码正确,我如何发现“已授予访问权限”被打印3次?相关的知识,希望对你有一定的参考价值。

如何在正确输入密码时停止打印“已授予访问权限”?当输入'箭鱼'时,授予的访问权限被打印三次

print ('The Database is password protected') # Says the Data base is password protected
print ('please enter password') #say please enter password

password = ('swordfish')
swordfish = 3

password = input()
if password == 'swordfish':

    print ('Access granted.')

else:

 if password != ('swordfish'):


    print ('wrong password.')

    print ('two attempts remain')

 else:
    password = input()

if password == 'swordfish':
    print ('Access granted.')
else:
    password = input()
if password == 'swordfish':
     print ('Access granted.')
else:
    print ('wrong password.')
    print ('one attempt remain')

    password = input()
    if password == 'swordfish':

if password != ('swordfish'):

     print ('You have been blocked from the database')
答案

如果你想阻止它被打印并清理你的代码,那么使用循环:

编辑:这是使用for-else代码块的最佳时机。在这个例子中,else子句只会在完整的for-loop已经用完时才会运行,即如果输入了正确的密码,它就不会运行“你已经被阻止了数据库”(因为那时break语句已执行)

password = 'swordfish'

print('The Database is password protected')

for attempts in range(2, -1, -1):
    if input('Please enter password') == password:
        print('Access granted.')
        break
    else:
        print('Wrong password.')
        print('%s attempts remain' % attempts)
else:
    print('You have been blocked from the database')
另一答案
print ('The Database is password protected')
print ('please enter password') 

password = "swordfish"

for i in range(3):
    attempt = input("Enter the password: ")
    if attempt == password:
        print("Access Granted")
        break
    else:
        print("Wrong password")
        print(2 - i, "attempt(s) reamin")

    if i == 2:
        print("You have been blocked from the database")
另一答案

你可以试试for循环:)

for e in range(3):
    a=input()
    if a=="swordfish":
        print("Access Granted")
        break
    else:
        print("Wrong Password. You have "+str(3-(e+1))+" attempts left")

以上是关于如果密码正确,我如何发现“已授予访问权限”被打印3次?的主要内容,如果未能解决你的问题,请参考以下文章

Google Cloud Data Fusion 无法访问其他项目的数据,即使已授予访问权限

如何*正确*存储密码?

简单的购物系统

pdf如何解除权限进行打印

无法在 Python 3.x 中登录

加盐密码哈希:如何正确使用