Logical error. But where?

import random

counter = 0
reshka = 0
orel = 0
monetka = random.randrange(100) + 1
while counter != 100:
 if monetka % 2:
 reshka += 1
else:
 orel += 1
 counter += 1
print("\nThere are ", reshka, "reshkas and" orel, "orels")


The result or 100 tails, or 100 eagles.
There are 100 reshkas and 0 orels
June 14th 19 at 20:08
3 answers
June 14th 19 at 20:10
Solution
You're a coin before entering the loop once cast, and then a hundred times looking at her.
Don't tell me how to do each time a new planting in a new iteration? - Chris.Barte commented on June 14th 19 at 20:13
to have this string in a loop, just under the line with the while loop. And don't forget the indent - well, Yes, otherwise the app will not start. - Benedict_Shiel commented on June 14th 19 at 20:16
June 14th 19 at 20:12
Solution
Well, there is one "coin", "throw". It is necessary for each iteration of the loop to do it.
On the flip came, and how to implement - no - Chris.Barte commented on June 14th 19 at 20:15
to transfer a string with "tossing" in the beginning of the cycle:
while counter != 100:
 monetka = random.randrange(100) + 1
- Benedict_Shiel commented on June 14th 19 at 20:18
, thank you) - Chris.Barte commented on June 14th 19 at 20:21
June 14th 19 at 20:14
Simplify your algorithm to one line:
from random import randint
n = 100

reshka = sum(randint(1, 100) % 2 for _ in range(n))

print("\nThere are ", reshka, "reshkas and ", n - reshka, "orels")


Or more simply:
sum(randint(0, 1) for _ in range(n))

Find more questions by tags Python