simeon9446
simeon9446

Reputation: 57

Python - Beginner Level - User input and while Loop combination

I have did a little research around "Google", "YouTube", "Facebook" and "Stack Overflow" and I haven't found what I was looking for. So, I need your guidance. :)

I want program to ask user input "PASSWORD" and every time user inputs wrong password the program asks password again, and again, and again until user types the correct password. Let's say that the password is as simple as "abc123".

So, I start the program and it asks to input: "PASSWORD: ". If user types "abc123" then program prints "GOOD PASSWORD". If user types anything what is not "abc123" then program prints "BAD PASSWORD". As simple as that.. for now.

My best attempt:

#SECTION1_ASKING
passwordInput=input('PASSWORD: ')
password='abc123'
while password == passwordInput:
    print('GOOD PASSWORD')
    break 
else:
    print('BAD PASSWORD')
    passwordInput=input('PASSWORD: ')

#SECTION2_RE-ASKING
while False:
    while password == paswordInput:
        print('GOOD PASSWORD')
    else:
        print('BAD PASSWORD')
        passwordInput=input('PASSWORD: ')

but I either make password asked once or twice or I stuck in Infinite while Loop.

Upvotes: 0

Views: 636

Answers (3)

cdarke
cdarke

Reputation: 44354

Here is my solution:

password = 'abc123'

while True:

    passwordInput = input('PASSWORD: ')

    if password == passwordInput:
        print('GOOD PASSWORD')
        break
    else:
        print('BAD PASSWORD')

How does that differ from yours? For a start you can see that I only have one call to input(), that's generally a good idea because then you only need to check the password in one place. Notice that I use if instead of while for the test.

In your second example you have while False:. Can this ever be True? No, so it won't get executed.

Notice as well that I use more whitespace. That does not slow the program down, it makes it easier to read. See also PEP008

Now you have it working, just for fun, consider an improvement. Normally you don't want the password to be visible when it is typed, but there's a module for that: getpass - it's part of the standard library so you don't need to download anything.

Add import getpass to the top of your script. Instead of input use getpass.getpass, in the same place in the program, with the same parameters. Now it won't echo the password entered by the user.

getpass is the module name, getpass is also a function name, so getpass.getpass('PASSWORD: ') means execute the getpass() function in the getpass module. We use lots of modules in Python, so its worth getting used to using them. You can find the documentation here, note there is also a getpass.getuser() to play with.

Upvotes: 0

Prakash Palnati
Prakash Palnati

Reputation: 3409

You can do as below in few lines.

password='abc123'
while(True):
    passwordInput=input('PASSWORD: ')
    if(passwordInput==password):
        print("Good password")
        break
    else:
        print("Bad password")

Upvotes: 1

Omi Harjani
Omi Harjani

Reputation: 840

Try this:

passwordInput=raw_input('PASSWORD: ')
password='abc123'
while True:
    if password == passwordInput:
        print('GOOD PASSWORD')
        break 
    else:
        print('BAD PASSWORD')
        passwordInput=raw_input('PASSWORD: ')

Upvotes: 2

Related Questions