# Help with a while loop

**URL:** <https://discuss.python.org/t/help-with-a-while-loop/45128>\
**Category:** Python Help\
**Created:** [February 5, 2024, 10:19pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128 "2024-02-05T22:19:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![supercoder](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@supercoder](https://discuss.python.org/u/supercoder)\
**Post date:** [February 5, 2024, 10:19pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/1 "2024-02-05T22:19:54Z")

</div>

I need help fixing up this while loop. When I place my answer I end up with the same answer “That is not A, B, C, D or E, try again:”

```python
print("1. What is the smallest unit in life? -'2points'-")
print("A: organs")
print("B: cells")
print("C: organism")
print("D: tissues")
print("E: organ system")
print("---------------")
organs = ("A")
cells = ("B")
organisms = ("C")
tissues = ("D")
organ_system = ("E")
answers = (organs, cells, organisms, tissues, organ_system)
quest1s = input("?: ")
while quest1s != (organs, cells, organisms, tissues, organ_system):
    word = input("That is not A, B, C, D or E, try again: ")
if quest1s == cells:
    print("Correct, lets go on to the next question.")
elif quest1s == (organs, organisms, tissues, organ_system):
    print("Incorrect, lets go on to the next question.")

```

---

<div class="post-metadata">

**Author:** ![bryevdv](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bryevdv/32/7424_2.png) [@bryevdv](https://discuss.python.org/u/bryevdv)\
**Post date:** [February 5, 2024, 10:29pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/2 "2024-02-05T22:29:16Z")

</div>

> [@supercoder](#):
>
> ```python
> quest1s = input("?: ")
> while quest1s != (organs, cells, organisms, tissues, organ_system):
> word = input("That is not A, B, C, D or E, try again: ")
> 
> ```

Two issues immediately visible:

The value of `quest1s` is a a _string_. But you are comparing equality against a _tuple_. This comparison will _never_ be equal. Perhaps you meant:

```python
while quest1s not in (organs, cells, organisms, tissues, organ_system):

```

The `not in` will check to see if a value is inside a container, i.e. is the _string_ one of the values _inside the tuple_.

But then also you store the original user input in a variable `quest1s` and then use check that variable for correctness in the while loop condition. But in case the first answer is wrong, you store the next user input in a completely new variable `word`—when the loop condition checks `quest1s` again, _it will not have changed_.

---

<div class="post-metadata">

**Author:** ![supercoder](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@supercoder](https://discuss.python.org/u/supercoder)\
**Post date:** [February 5, 2024, 10:36pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/3 "2024-02-05T22:36:46Z")

</div>

then how do I use the while loop.

---

<div class="post-metadata">

**Author:** ![supercoder](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@supercoder](https://discuss.python.org/u/supercoder)\
**Post date:** [February 5, 2024, 11:23pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/4 "2024-02-05T23:23:43Z")

</div>

i really want to use it

---

<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:** [February 5, 2024, 11:31pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/5 "2024-02-05T23:31:29Z")

</div>

The loop’s condition tests `quest1s`, but the code in the loop doesn’t change `quest1s`, it puts the reply in `word`.

---

<div class="post-metadata">

**Author:** ![bryevdv](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bryevdv/32/7424_2.png) [@bryevdv](https://discuss.python.org/u/bryevdv)\
**Post date:** [February 5, 2024, 11:39pm UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/6 "2024-02-05T23:39:06Z")

</div>

@supercoder My reply was fairly explicit, in terms of what problems there are in the code, and what potential ways there are to fix those problems. If that’s not enough to go on, then I think that’s an indication that you would benefit from taking a step back at this point, and spending some time going through some tutorials. The Python docs themselves have an extensive one:

> **[The Python Tutorial](https://docs.python.org/3/tutorial/index.html)**
>
> Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...

But there are many more online under a Google search for “Python tutorial” e.g. if a video tutorial is more your preference.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [February 6, 2024, 3:20am UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/7 "2024-02-06T03:20:54Z")

</div>

Hi,

I think that this is what you are looking for:

```python
print("1. What is the smallest unit in life? -'2points'-")
print("A: organs")
print("B: cells")
print("C: organism")
print("D: tissues")
print("E: organ system")
print("---------------")

answers = ('A', 'B', 'C', 'D', 'E')

quest1s = input("?: ")

while True:

    if quest1s == 'B':
        
        print("Correct, lets go on to the next question.")
        break

    elif quest1s not in answers:
        
        quest1s = input("That is not A, B, C, D or E, try again: ")

    elif quest1s != 'B':
        
        print("Incorrect, lets go on to the next question.")
        break
        

```

When writing code, make sure you allow spaces between distinct parts of your code. It makes it much more readable not just for your own review, but for others as well. It takes away that cluttery look.

---

<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:** [February 6, 2024, 3:26am UTC](https://discuss.python.org/t/help-with-a-while-loop/45128/8 "2024-02-06T03:26:11Z")

</div>

That can be shortened and simplified:

```python
print("1. What is the smallest unit in life? -'2points'-")
print("A: organs")
print("B: cells")
print("C: organism")
print("D: tissues")
print("E: organ system")
print("---------------")

answers = ('A', 'B', 'C', 'D', 'E')

quest1s = input("?: ")

while quest1s not in answers:
    quest1s = input("That is not A, B, C, D or E, try again: ")

if quest1s == 'B':
    print("Correct, lets go on to the next question.")
else:
    print("Incorrect, lets go on to the next question.")

```
