0

Im new to python and programming in general and I came across this problem while fiddling around with a simple while loop. The loop takes input to evaluate two possible passwords:

    print('Enter password')
    passEntry = input()

    while passEntry !='juice' or 'juice2':
      print('Access Denied')
      passEntry = input()
      print(passEntry)

    print('Access Granted')

It doesnt seem to be accepting juice or juice2 as valid.

Also just accepting one password like:

    while passEntry != 'juice' :

will not work, while:

    while passEntry !='juice' :

works fine. I cant seem to find the reason for these issues (Only difference between the latter two is the space after the =). Any help is appreciated.

1
  • This may not reason of error I guess! Commented Dec 28, 2012 at 5:49

6 Answers 6

7

First, you should use Python's getpass module to get a password portably. For example:

import getpass
passEntry = getpass.getpass("Enter password")

Then, the code you have written to guard the while loop:

while passEntry != 'juice' or 'juice2':

gets interpreted by the Python interpreter as a while loop with the guard expression

(passEntry != 'juice') or 'juice2'

This is always true because regardless of whether passEntry equals "juice" or not, "juice2" will be considered as true when interpreted as a boolean.

In Python, the best way to test membership is to use the in operator, which works for a variety of data types such as a list or a set or a tuple. For example, a list:

while passEntry not in ['juice', 'juice2']:
Sign up to request clarification or add additional context in comments.

Comments

3

you can use

while passEntry not in ['juice' ,'juice2']:

Comments

1

How about:

while passEntry !='juice' and passEntry!= 'juice2':

and using raw_input() instead of input()?

input() evaluates the input as if it were Python code.

1 Comment

Yes this was it for the first part, made logical error using or instead of and. Thanks
1

passEntry !='juice' or 'juice2' means (pass != 'juice') or ('juice2'). "juice2" is a nonempty string, so it is always true. Thus your condition is always true.

You want to do passEntry != 'juice' and passEntry != 'juice2', or more nicely passEntry not in ('juice', 'juice2').

Comments

0

Does this work?

while passEntry !='juice' and passEntry !='juice2':

Comments

0

Your error is in the way you wrote the while statement.

while passEntry !='juice' or 'juice2':

That line will always be true, when read by the python interpreter. And also instead of:

passEntry = input()

Use:

passEntry = raw_input()

(Unless you are using Python 3)

the input in Python 2 evals your input.

This will be the proper code:

print('Enter password')
passEntry = raw_input()

while passEntry != 'juice' and passEntry != 'juice2':
    print('Access Denied')
    passEntry = raw_input()
    print(passEntry)

print('Access Granted')

1 Comment

Ok, im running python3, so i guess thats why raw_input() gave me problems. Thanks

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.