christopher bin
christopher bin

Reputation: 1

How to put an input after a loop have ended?

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

Answers (3)

Vineet Kumar Doshi
Vineet Kumar Doshi

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

Martin Evans
Martin Evans

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

Yu Hao
Yu Hao

Reputation: 122503

Check counter to decide what to print:

else:
    counter += 1
    if counter < 3:
        print "try again"
    else:
        print "bye"

Upvotes: 1

Related Questions