0

I'm trying to make a small program which can tell whether or not the correct password has been entered and keep asking for the correct password until it is entered. The correct password is 'Secret'.

I get as far as creating my while loop. It will ask for the password and ask to enter it again, but it will do this regardless of whether you enter the correct password first time round or not. Where am I going wrong? And how can I get it to break if the correct password is entered first time and how do I keep it asking for the password until it is entered correctly?

This is my code so far:

password = raw_input('What is the password? ')
correctPassword = 'Secret'
tryAgain = raw_input ('Enter password again ')

password
while password == False:
    print 'Enter password again '
    if password == correctPassword:
        print 'You are in!'
        break
1
  • There is no syntax error. Try to comment your code in plain English and see whether it does what you want it to. Commented Oct 17, 2012 at 11:51

3 Answers 3

6

Try the below code: -

password= raw_input('What is the password? ')
correctPassword= 'Secret'

while password != correctPassword:
    password = raw_input ('Enter password again ')

print "Success"

This will execute the while loop until the password input in the while loop is not equal to the correct password.

Sign up to request clarification or add additional context in comments.

3 Comments

@pazza : This is a good solution given by rohit. What you need to do is u have to compare your password with the correct pass word string. Only then you can say whether its correct or not .Upvoted
Thank you very much! What does the != tell it to do?
@PazzaPythonNewbie Its telling while password is not equal to correctPassword.
1

Here is the right code.

correctPassword= 'Secret'
while True:
    password= raw_input('What is the password? ')
    if password == correctPassword:
        print 'You are in!'
        break
    print 'Enter password again '

1 Comment

Your code is incorrect. Try entering the correct password...you have to do it three times, before you 'get in.'
0

Here is my code

password = 'password'
Get_password = input("Enter the password: ")
while Get_password != password:
     print("the password you entered is not correct, enter the correct   password")
     break
print("You are logged in!")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.