# "While" loop only running one or no "if" statements

I am trying to make a single celled organism simulator, with exponential growth. The code I have made uses strings and “while” loops, that are very inconsistent.

I highly recommend formatting the code as text using a code block (three back-ticks, ```), rather than taking a picture of your monitor and posting that. It makes it much easier for people to read.

`dice` is always going to be a list of random integers. `dice.count(x)` is always going to be an integer (the number of times `x` appears in the list). These two things will never be equal, so your `if` statements are never `True`.

Can you write out what you want this code to do using pseudo-code (that is, basically describe the behavior in English, but structured step-by-step)? It’s not clear to me how you want this to work.

‘’‘dice = list of random numbers ranging 1-10,
amount of numbers determined by number of organisms.
blobs = number of organisms
ticks = how many times the loop has played
kids = number of times a blob has procreated
Each blob has a 20% chance to procreate (+ 1 blobs) (+ 1 kids) (+ 1 ticks),
10% chance to die (- 1 blobs) (+ 1 ticks),
and 70% chance for nothing to happen (+ 1 ticks).
The amount of times 1 appears on the list, a blob dies.
The amount of times a 2 or 3 appears on the list, a new blob is born.
Otherwise, only the ticks variable increases by 1.
The loops ends when the ticks variable reaches 100, or all blobs die.’‘’
Let me know if this explanation is not helpful enough, and thank you for helping me.

Aha, the part that was confusing me was how you were calculating randomness. For each blob you generate a number from 1 to 10 and you want to note when a value is 1 for deaths or `2 or 3` for births. That’s not how I would generate randomness here but it makes sense.

Like I said above: your `if` statements are not testing for the right thing. We can break down the first one:

``````           / checks if a list == an int, never true
________________
if dice == dice.count(2) or dice.count(3)
-------------
\ checks if 3 appears in the list,
true if count > 0
``````

It’s important to note that `==` has priority over `or`, so you’re checking `dice == dice.count(2)` and `bool(dice.count(3))` and then `or`ing the results. This will only evaluate to true if 3 appears in the list `dice` one or more times.

The second `if` statement has the same problem at the first, `dice` is a list and is never equal to an integer.

More generally, I don’t think this is going to do what you want. You only add one birth and one death per loop, but you generated an outcome for every blob. I suspect you want to go through each result in `dice` and evaluate the outcome:

``````for outcome in dice:
if outcome == 1:
... this blob reproduced ...
elif outcome == 2 or outcome == 3:
... this blob died ...
``````

As an aside, I’ll just point you to this function in the `random` module which would let you generate random outcomes in a more intuitive way with the `weights` keyword.

1 Like

Thank you, this helped a lot!

Backticks are the ` symbol, not ’ (a quotation mark). On a US keyboard, to type `, use the key above the tab key, to the left of the 1 key. Make sure to put the backticks on separate lines, not added to the first and last lines of the code.

I can’t understand what you mean by this. Can you give some examples of what the list could look like, what should happen as a result, and how you determine that? It seems like you want something to happen perhaps multiple times. `if` does not make sense to use for a “how many” question.