-1

Why can't I break out of the first while loop when I type in joe or Joe?

joe = ''
password = ''
while joe != 'Joe' or 'joe':
    print('Who are you?')
    joe = input()
while password != 'swordfish' or 'Swordfish':
    if joe == 'joe' or 'Joe':
        print('What is the password? (Hint: its a fish)')
        password = input()

if joe == 'Joe' or 'joe' and password == 'swordfish' or 'Swordfish':
    print('Access Granted')
else:
    print('Access Denied')
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • 2
    This `if joe == 'joe' or 'Joe'` doesn't mean what you think it means. It means `if (joe == 'joe') or bool('Joe')` and `bool('Joe')` always evaluates to `True` which means the if-statement always passes. You probably meant `if joe in ('joe', 'Joe')`. Or even better `if joe.lower() == 'joe'`. – Steven Rumbalski Jul 07 '17 at 17:28
  • For the condition in the `while` loop, try `joe != 'Joe' or joe != 'joe'`. Right now, the while loop will continue running while either `joe` does not equal `'Joe'` or simply `'joe'`. `'joe'` is a string, and its boolean value is always `True`. Thus, whether `joe` is equal to `'Joe'` or not, the string `'joe'` will always evaluate to `True` and the `while` loop will never break. – victor Jul 07 '17 at 17:30
  • Hey! Thank you for the help. After I posted this, I took a look at what I had again. (I guess a break does the mind good) and I saw that I didn't have the variable written twice. You're right 'Joe' without joe == 'Joe' has no meaning in the while loop. I was using the wrong operator as well. It should be an "and" operator not an or. Thank you for the .lower() tip, this would shorten the code up and make it more efficient! :) – Andrew Hojnowski Jul 07 '17 at 21:35

1 Answers1

1

Try this:

val = ''
password = ''
while True:
    print('Who are you?')
    val = input()

    if val.lower() == 'joe':
        break
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Hey! Thanks for the help! I just thought it out logically and I am supposed to be using the "and" operator not "or" lol Going off my C++ knowledge, does value.lower make all characters in the string lowercase even if the input has an uppercase? – Andrew Hojnowski Jul 07 '17 at 21:31
  • @AndrewHojnowski you are correct. `value.lower()` will turn all characters in the string to lowercase, even if there is an uppercase in the string. For instance, `val = "Hello"`, `val.lower()` will output `"hello"` – Ajax1234 Jul 07 '17 at 21:50