# While loop with 2 conditions problem

**URL:** <https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 25, 2022, 11:44am UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308 "2022-10-25T11:44:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitalKETCHUP](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@digitalKETCHUP](https://discuss.python.org/u/digitalKETCHUP)\
**Post date:** [October 25, 2022, 11:44am UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/1 "2022-10-25T11:44:03Z")

</div>

Hello, I’m new to python and code in general. I’m trying to create a quiz game as my first project. Because of this, I’m adding a bunch of other things like adding your name for the game to call you throughout the session. I also ask the gender so the game knows wether to call you mr/mrs.  
my code looks something like this:

```auto
> print('Please enter your gender.')
> gender = input().lower()
> 
> 
> 
> if gender == 'male':
> print('Nice to meet you Mr.', last_name, '!')
> main_menu()
> if gender == 'female':
> print('Nice to meet you Ms.', last_name, '!')
> main_menu()
> 
> 
> def gen_error():
> print("I'm sorry,I did not understand that.)
> print('Enter "MALE" for male or "FEMALE" for female.')
> 
> 
> while gender != ('male' or 'female'):
> gen_error()
> gender_input = input().lower()
> if gender_input == 'female':
> break

```

This is the only way that i have gotten it to work close to how i want it but the problem is, when i type in ‘female’, it still calls the “gen\_error” message. Just wondering if anyone knows a way to fix this or an easier way to do it altogether. Thanks!

---

<div class="post-metadata">

**Author:** ![Melendowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/melendowski/32/2960_2.png) [@Melendowski](https://discuss.python.org/u/Melendowski)\
**Post date:** [October 25, 2022, 12:29pm UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/2 "2022-10-25T12:29:54Z")

</div>

You have to breakdown that condition in the `while` loop to see what it’s doing.

So in your repl do this

```auto
x = "male" or "female"
gender = "male"
print(gender != x)
gender = "female"
print(gender != x)

```

Notice `x` is always going to be `"male"`. This is because `or` when used in this context will assign the first truthy object to `x`. `str` is a subclass of `collections.Sequence` whose ` __bool__ ` implementation defers to ` __len__ ` for deciding if `True` or `False` should be returned. So a sequence of length 0 returns `False` and a sequence of non zero length returns `True`.

let’s see demonstrate some other examples

```auto
print("" or "male")
print("male" or "")
print("female" or "male")

```

From the python docs

[https://docs.python.org/3/reference/expressions.html#boolean-operations](https://docs.python.org/3/reference/expressions.html#boolean-operations)

Now to provide you with a solution

`!=` does not distribute over `or` in python, as I believe I demonstrated above. What you must do is split that into a disjunction of not equal operations or more concisely using a generator expression or even more concisely using a containment check

`gender != "male" and gender != "female"`  
`not any(gender == x for x in ("male", "female"))`  
`gender not in {"male", "female"}`

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [October 25, 2022, 6:01pm UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/3 "2022-10-25T18:01:03Z")

</div>

FYI, `gender != "male" or gender != "female"` is always `True`. What you mean is `gender != "male" and gender != "female"`.

---

<div class="post-metadata">

**Author:** ![Melendowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/melendowski/32/2960_2.png) [@Melendowski](https://discuss.python.org/u/Melendowski)\
**Post date:** [October 25, 2022, 10:07pm UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/4 "2022-10-25T22:07:44Z")

</div>

Dummy me! Completely forgot DeMorgan’s

---

<div class="post-metadata">

**Author:** ![digitalKETCHUP](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@digitalKETCHUP](https://discuss.python.org/u/digitalKETCHUP)\
**Post date:** [October 26, 2022, 3:05am UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/5 "2022-10-26T03:05:41Z")

</div>

Thank you! But I’ll be completely honest, the first time I ever touched python or anything to do with code at all was 2 days ago. So while I understand some functions and how some things work, that explanation went over my head. Thank you for trying though. 😂

---

<div class="post-metadata">

**Author:** ![digitalKETCHUP](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@digitalKETCHUP](https://discuss.python.org/u/digitalKETCHUP)\
**Post date:** [October 26, 2022, 3:07am UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/6 "2022-10-26T03:07:31Z")

</div>

I understand the part about returning with a zero or non zero and that’s why I figured it didn’t work but I don’t understand at the end when you give the solution

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [October 26, 2022, 6:01am UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/7 "2022-10-26T06:01:21Z")

</div>

A suggestion for part of your code:

```auto
salutations = {'male': 'Mr.', 'female': 'Ms.', 'other': 'M.'}
gender = input(f"Enter one of ({', '.join(salutations)}): ")

```

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 26, 2022, 7:28am UTC](https://discuss.python.org/t/while-loop-with-2-conditions-problem/20308/8 "2022-10-26T07:28:04Z")

</div>

> [@digitalKETCHUP](#):
>
> …that explanation went over my head.

I will try to explain it.

Before the statement below is encountered the variable `gender` has been assigned the text entered by user.

```python
while gender != ('male' or 'female'):
    ...

```

The part `gender != ('male' or 'female')` is an expression which is evaluated as `True` or `False`.

Let’s analyze the expression. Like in mathematics first the expression inside parentheses has to be evaluated:  
`'male' or 'female'`  
The operator `or` is a boolean operator. It evaluates its operands as truthy or falsy. Here the operands are strings which are falsy when empty. Otherwise they are truthy. So here both operands are regarded as `True`.

```python
>>> bool('')
False
>>> bool('male')
True

```

The operator `or` has one more property - it performs shortcut evaluation. It means when the result is known from the first operand, the second one is not evaluated. This is our case - the first operand is truthy so the result must be truthy regardless the second operand.

…but `or` has yet one more property. The result of the operation is the last operand evaluated. So in our case the result is `'male'` (the second operand `'female'` did not need to be evaluated):

```python
>>> 'male' or 'female'
'male'

```

Let’s substitute the result for the expression’s part in parentheses:

```auto
gender != 'male'

```

Now I think you understand what the expression does. When you are in doubts I recommend you to deconstruct your expression to pieces and evaluate the pieces separately.
