0

So Heres My Code

while True:
    username = raw_input("Username:")
    time.sleep(1)
    print username
    qwerty = raw_input("Is this right?")
    if qwerty == 'yes' or "Yes" or "Yeah" or "yeah" or "yup" or "Yup":
        print "OK."
        break
    elif qwerty == 'no' or 'No' or 'nope' or 'Nope' or 'nah' or 'Nah':
        print "Please type your Username again"
        continue
    else:
        print "Please Try a more common answer"
        continue`

I Don't Know Whats wrong because what ever i type for the input it only comes out with the first Option. does anyone know why?

msvalkon
  • 11,887
  • 2
  • 42
  • 38
DuckyQuack
  • 39
  • 10
  • `qwerty == 'yes' or "Yes"` doesn't check if qwerty is yes or Yes. It only checks if qwerty == 'yes', if not then next condition is `if "Yes"` which is always true. So you first condition is always true. – Priyesh Kumar Oct 19 '16 at 20:07
  • You can also use parentheses `if (qwerty == 'yes') or (qwerty == "Yes"):`. Not a very efficient solution in your case since you have so many tests to do, but maybe in other solutions it'll be helpful – Rafael Oct 19 '16 at 20:10

1 Answers1

2

You need to update your condition in if as:

if qwerty in ['yes', "Yes", "Yeah", "yeah", "yup", "Yup"]

Explanation:

or in Python performs Logic OR operation. If value is True, the condition is satisfied, else it goes to next condition in or. To give you more clarity. For example:

>>> 'Hello' or 'Man'
'Hello'
>>> '' or 'Man'
'Man'

Also, note that python treats non-zero and non-empty string value as True.

Example with your case:

>>> querty = 'Yes'
>>> querty == 'yes'  # This returns False
False
>>> querty == 'yes' or 'Yes'  # Goes to next condition since 1st is False 
'Yes'                         # Since it is non-empty string, retuns that value
                              # and 'if' treats that as True
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Or do `if qwerty == 'yes' or qwerty == 'Yes' or qwerty == 'Yeah' or ...` The only difference is speed vs. readability. I like the `if qwerty in ['yes', 'Yes', ...]` solution better, but it may be slower. The only way to know for sure is to benchmark it. But benchmarking it would probably take more effort that it's worth. – erapert Oct 19 '16 at 20:07
  • wow thanks! It was really helpful (And extremely fast) – DuckyQuack Oct 19 '16 at 20:14
  • 1
    `qwerty.lower()` will simplify the list as well, as then you only need to compare lowercase answers. personally, I'd just do `if qwerty[0].lower() == 'y':` - it would accept anything starting with y – TemporalWolf Oct 19 '16 at 20:14
  • @DuckyQuack: If it helped, mark it as accepted :) . Also, check the comments given by others, you will get to know the alternatives and will clear your few doubts. – Moinuddin Quadri Oct 19 '16 at 20:18
  • 1
    Interestingly, according to timeit, the fastest solution is: `qwerty[0] == 'Y' or qwerty[0] == 'y'`. This executes in about half the time of the others. The next fastest is `qwerty in ['yes', "Yes", "Yeah", "yeah", "yup", "Yup"]` – TemporalWolf Oct 19 '16 at 20:20
  • @TemporalWolf: But my `timeit` shows different result. For example: `'a' in ['a']` is faster than `'a' == 'a'`. Infact, `'answer' in ['answer']` is faster than `'a' == 'a'` – Moinuddin Quadri Oct 19 '16 at 20:27
  • 1
    @anonymous It is marginally faster (<5% in my timeit) with a single element **if** it evaluates `True`. When `False`, it takes about 30% longer. So it depends on your input data which would be faster overall. – TemporalWolf Oct 19 '16 at 20:33