On 12/02/14 15:25, Marc Eymard wrote:
However, I still don't understand the bug since, in my understanding, both files are incrementing variable *count_flips* each time until the loop becomes false.
> count_heads = 0 > count_tails = 0 > count_flips = 0 > > while count_flips != 100: This is usually a bad idea since if count_flips gets incremented by 2 it could go over 100 and this test will still be true and it will loop forever... (see below). Its better to have a test like: while count_flips <100: > coin_side = random.randint(1,2) > > if coin_side == 1: > count_heads += 1 > count_flips += 1 > else: count_tails += 1 > > count_flips += 1 Because this is outside the else block it gets incremented twice every time a 1 gets 'thrown'. (see above) That's why your solution of incrementing only once before the if/else tests is better. Its a more accurate implementation of the logic and avoids the double increment problem with the while loop. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor