dimanche 6 décembre 2020

how else works in python?

I'm writing a code where output depends on previous input. Let's imagine I choose magic stones which are labeled 0 and 1 and I pick them from a sack. For the first pick chances that I get 0 or 1 are 50/50 but if I pick stone 1, chances that I'll pick stone 1 next is 70% and chances that I pick stone 0 is 30%. If I pick stone 0, there is 60% chance that I pick stone 0 next time and 40% chance that I pick stone 1. Here's some code

import random
ones = 0
zeros = 0
a = random.randint(0 , 100)
stone = 0
if a > 50: 
    stone = 1
else: 
    stone = 0
for i in range(100):
    if stone == 1:
        ones+= 1
        a = random.randint(0 , 100)
        if a > 30:
            stone = 1
        else:
            stone = 0
    if stone == 0:
        zeros+= 1
        a = random.randint(0 , 100)
        if a > 40:
            stone = 0
        else:
            stone = 1
        
    
print( ones)
print(zeros)

The problem is that ones and zeros don't add up to 100 but if I replace 'if stone == 0' with 'else', everything works fine. Why does such strange thing happen? Aren't those two conditions the same?

Aucun commentaire:

Enregistrer un commentaire