Need Help Putting The Else Into The While With A Counter
password = str() while password != 'changeme': password = input('Password: ') print('Thou Shall Pass Into Mordor') else print('Thou Shall Not Pass Into Mordor') Could I pleas
Solution 1:
Use break
to end a loop, and use for
with a range()
:
for attempt inrange(5):
password = input("Password: ")
if password == "changeme":
print("Thou Shall Pass Into Mordor")
breakelse:
print("Thou Shall Not Pass Into Mordor")
The else
branch of a for
loop is only executed when you did not use break
to end the loop.
Demo:
>>># Five failed attempts...>>>for attempt inrange(5):... password = input("Password: ")...if password == "changeme":...print("Thou Shall Pass Into Mordor")...break...else:...print("Thou Shall Not Pass Into Mordor")...
Password: You shall not pass!
Password: One doesn't simply walk into Mordor!
Password: That sword was broken!
Password: It has been remade!
Password: <whispered> Toss me!
Thou Shall Not Pass Into Mordor
>>># Successful attempt after one failure...>>>for attempt inrange(5):... password = input("Password: ")...if password == "changeme":...print("Thou Shall Pass Into Mordor")...break...else:...print("Thou Shall Not Pass Into Mordor")...
Password: They come in pints?! I'm having one!
Password: changeme
Thou Shall Pass Into Mordor
Post a Comment for "Need Help Putting The Else Into The While With A Counter"