Throws conditional coin 100 times. Counts how many times the eagle (front) fell, how much tails (back).

The problem is that with the successful completion of one cycle, the random variable does not change. What to do?

 import random throw = 0 front = 0 back = 0 random = random.randint(1, 2) while throw != 100: if random == 1: front += 1 elif random == 2: back += 1 throw += 1 print(back) print(front) print(throw) input("\n\nНажмите Enter, чтобы выйти") 
  • 3
    You gave the name of the imported module random to the variable in which you recorded the value random.randint(1, 2) . Therefore, even putting the expression random = random.randint(1, 2) into the body of the loop, you get an error. After all, an object of type int (which returns random.randint(1, 2) ) does not have a randint method. Think of another name for the variable (instead of random , for example, var ) and enter the expression var = random.randint(1, 2) into the loop body. - mkkik

3 answers 3

 import random throw = 100 front = list(random.randint(0, 1) for _ in range(throw)).count(1) back = throw - front print(front, back) 

51 49

    Getting the result of random.randint needs to be random.randint ...

    • one
      how? I tried before the if block, after the if block, and so on. The compiler swears. - Artem

    Perhaps you should make a call to the random.randint function (1, 2) inside the loop too:

     import random throw = 0 front = 0 back = 0 while throw != 100: if random.randint(1, 2) == 1: front += 1 else: back += 1 throw += 1 print(back) print(front) print(throw) input("\n\nНажмите Enter, чтобы выйти") 
    • I tried this code myself, but for some reason it does not work. Yours too. - Artem