0
keyCounter = 0
key1Value = 0
key2Value = 0
key3Value = 0

print(key1Value)

key1Value = input("Press the first key.")
key2Value = input("Press the second key.")
key3Value = input("Press the third key.")
# password = 123

if key1Value == 1 and key2Value == 2 and key3Value == 3:
    print("Access Granted")
    print(key1Value)
    print(key2Value)
    print(key3Value)
elif   key1Value != 1 and \
       key2Value != 2 and \
       key3Value != 3:
       print("Access Denied")
       print(key1Value)
       print(key2Value)
       print(key3Value)
else:
    print("Vault error")
    print(key1Value)
    print(key2Value)
    print(key3Value)

input("Press Enter to continue...")

Why does this always result in "Vault Error" ? I have looked around and I feel like my if conditions are wrong but I'm not sure.

Eddy Vinck
  • 410
  • 1
  • 4
  • 16
  • Because `input` always returns a string. In other words you're trying to compare `1` to `'1'`. Use `int(input("Press the first key."))`. – roganjosh Feb 15 '17 at 15:59
  • 3
    If you enter in a code like `126` (were 2 of the values match, but one doesn't) you will get a "Vault Error" instead of "Access Denied". You will only see "Access Denied" if *none* of the numbers match. I'm not sure if that's the outcome you are looking for. This may not be a problem here, but I just wanted to point out that the inverse of: `key1Value == 1 and key2Value == 2 and key3Value == 3` is actually `key1Value != 1 or key2Value != 2 or key3Value != 3` (This is due to [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws)). – gen_Eric Feb 15 '17 at 16:04

1 Answers1

0

The input method return the user input in a type of string, and you try to compare two different of types (int and str)

Change the comparisons to str, for example:

if key1Value == "1" and key2Value == "2" and key3Value == "3":

You can also cast the input into int:

key1Value = int(input("Press the first key."))

Notice that you will get an error when you won't insert an actual number.

In conclusion, I would do the following:

try:
    value=int(input("Press the first key."))
except ValueError:
    print("This is not a whole number.")

This way, you can check whether the user input is type of int, and if it's not an int type you can handle it in an appropriate way.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58