Reputation: 1
counter=0
actual_password="hi"
enter_password=''
while enter_password!=actual_password and counter<3:
enter_password=raw_input("pls enter pass")
if enter_password==actual_password:
print "well done"
else:
print "try again"
counter += 1
How to print bye at the end of loop after three tries?
Upvotes: 0
Views: 61
Reputation: 4900
This may help ... simple if
statement.
counter=0
actual_password="hi"
enter_password=''
while enter_password!=actual_password and counter<3:
enter_password=raw_input("pls enter pass")
if enter_password==actual_password:
print "well done"
else:
counter += 1
if counter == 3:
print 'bye'
break
print "try again"
Upvotes: 2
Reputation: 46789
This kind of problem is well suited to using a for
loop. This would print "bye" after the three tries, or if the user enters the correct password.
actual_password = "hi"
for tries in range(3):
enter_password = raw_input("pls enter pass: ")
if enter_password == actual_password:
print "well done"
break
print "try again"
print "bye"
Upvotes: 0
Reputation: 122503
Check counter
to decide what to print:
else:
counter += 1
if counter < 3:
print "try again"
else:
print "bye"
Upvotes: 1