python - Coinflip bot infinite loop -
i trying create infinite loop coin flip bot keep pumping out results, totally new python lost. have done reading looping , nothing jumping out @ me.
thank help.
from random import randint import random heads = 0 tails = 0 cointoss = 0 while true: coinresult = random.randint(1,2) cointoss +=1 #end loop if cointoss greater 100 if cointoss > 101: break if coinresult == 1: heads +=1 cointoss +=1 elif coinresult == 2: tails +=1 cointoss +=1 print("heads came up", heads, "times") print("tails came up", tails, "times")
your exact code of equivivalent to
import random def coinflip(maxtosses=-1): tosses = 0 while tosses != maxtosses: tosses += 1 yield random.choice([true, false]) heads, tails = 0 toss in coinflip(100): if toss: heads += 1 else: tails += 1
if wish infinite loop - pass nothing or negative value parameter coinflip
function. way, however, won't able ever stop (well, unless interrupt in ctrl+c or that).
coinflip
generator expression, can tricks interrupt infinite loop depending on condition.
Comments
Post a Comment