If I read your problem correctly, you need to provide a method to compensate for the broken coin producing a relatively fair amount of results. With that instead of calling the broken_coin() function every time directly, one could call a function that calls the function and every other time returns the reverse result to the calling function.
After reading the comments about what could or could not be used, I’ve updated my sample code.
import random
def broken_coin():
if random.random() <= 0.9:
return "Heads"
return "Tails"
def fix_coin(j):
if j == 0:
return broken_coin()
coin = broken_coin()
if (coin == "Heads"):
return "Tails"
else:
return "Heads"
for x in range (100):
print(fix_coin(x %2))
See if this more closely fulfils the spirit of the problem.
Regards.
9
solved “The cheater’s coin” python riddle [closed]