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

问题描述 投票:2回答: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')
python python-3.x python-2.7 loops input
3个回答
3
投票

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

编辑:这是使用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')

0
投票
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")

0
投票

你可以试试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")
© www.soinside.com 2019 - 2024. All rights reserved.