[Tutor] beginner here
Im trying to write this program where i make it flip a coin 100 times then tells me the number of heads and tails it came up with. this is what I got but it does not run # Coin Flip # Demonstrating the While loop import random print(I will flip a coin 100 times and tell you) print(, how many heads and how many tails I got.) coin_rolls = random.randint(1,2) heads = 0 tails = 0 rolls = 0 if rolls = 100: rolls += 1 elif coin_rolls == 1: heads += 1 elif coin_rolls == 2: tails += 1 else: print(error) print(There was , heads, rolls for heads,) print(\nand there was , tails, rolls for tails.) input(\n\nPress the enter key to exit.) I end up with 0 rolls for heads and 0 rolls for tails...I have made attempts to put the coin_rolls = random.randint(1,2) within the loops but only end up with errors what wrong with my code?___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner here
You are using an 'elif' for your 'coin_rolls == 1:'. The 'elif' keyword means that if the last 'if' statement (and any 'elif's behind it) was *not* true, only then will it be executed. Your code could be written as 'if rolls is NOT less than or equal to 100, only then check to see if it is 1 or 2'. Replace your first 'elif' with 'if', and it should work. On Wed, Dec 7, 2011 at 8:17 PM, Do youknow who mrsann...@yahoo.com wrote: Im trying to write this program where i make it flip a coin 100 times then tells me the number of heads and tails it came up with. this is what I got but it does not run # Coin Flip # Demonstrating the While loop import random print(I will flip a coin 100 times and tell you) print(, how many heads and how many tails I got.) coin_rolls = random.randint(1,2) heads = 0 tails = 0 rolls = 0 if rolls = 100: rolls += 1 elif coin_rolls == 1: heads += 1 elif coin_rolls == 2: tails += 1 else: print(error) print(There was , heads, rolls for heads,) print(\nand there was , tails, rolls for tails.) input(\n\nPress the enter key to exit.) I end up with 0 rolls for heads and 0 rolls for tails...I have made attempts to put the coin_rolls = random.randint(1,2) within the loops but only end up with errors what wrong with my code? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner here
On 12/07/2011 08:39 PM, Max S. wrote: You are using an 'elif' for your 'coin_rolls == 1:'. The 'elif' keyword means that if the last 'if' statement (and any 'elif's behind it) was *not* true, only then will it be executed. Your code could be written as 'if rolls is NOT less than or equal to 100, only then check to see if it is 1 or 2'. Replace your first 'elif' with 'if', and it should work. You top-posted. On this forum, you should put your comments after whatever you're quoting. On Wed, Dec 7, 2011 at 8:17 PM, Do youknow whomrsann...@yahoo.com wrote: Im trying to write this program where i make it flip a coin 100 times then tells me the number of heads and tails it came up with. this is what I got but it does not run # Coin Flip # Demonstrating the While loop import random print(I will flip a coin 100 times and tell you) print(, how many heads and how many tails I got.) coin_rolls = random.randint(1,2) heads = 0 tails = 0 rolls = 0 if rolls= 100: rolls += 1 elif coin_rolls == 1: heads += 1 elif coin_rolls == 2: tails += 1 else: print(error) print(There was , heads, rolls for heads,) print(\nand there was , tails, rolls for tails.) input(\n\nPress the enter key to exit.) I end up with 0 rolls for heads and 0 rolls for tails...I have made attempts to put the coin_rolls = random.randint(1,2) within the loops but only end up with errors what wrong with my code? First point is if you're getting errors, you should post the error message, including the full traceback. Or if you get no errors, but the results aren't what you expect, you should indicate what you expected, and how the actual results were different than expected. What you expect is that rolls will be == 100, and that heads and tails will add up to 100. In addition to Max's point, there's another glaring problem here. You don't have a loop of any kind. Your comment indicates it's going to be a while loop, but since you already know how many times you're going to loop, there's no point. Just make it a for loop. for roll in range(100): Then of course the body of the loop will need to be indented. So you have to decide what's going to be inside the loop, and what's initialization, or summary stuff. You will then discover that the order of your code isn't quite right. Work on those two points, and see what you come up with. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner here
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 08-Dec-2011, at 6:47 AM, Do youknow who wrote: Im trying to write this program where i make it flip a coin 100 times then tells me the number of heads and tails it came up with. this is what I got but it does not run # Coin Flip # Demonstrating the While loop import random print(I will flip a coin 100 times and tell you) print(, how many heads and how many tails I got.) coin_rolls = random.randint(1,2) heads = 0 tails = 0 rolls = 0 if rolls = 100: rolls += 1 elif coin_rolls == 1: heads += 1 elif coin_rolls == 2: tails += 1 else: print(error) print(There was , heads, rolls for heads,) print(\nand there was , tails, rolls for tails.) input(\n\nPress the enter key to exit.) I end up with 0 rolls for heads and 0 rolls for tails...I have made attempts to put the coin_rolls = random.randint(1,2) within the loops but only end up with errors what wrong with my code? # head(h) --- even number # tail(t) --- odd number # roll(r) --- number of rolls === import random r = 100 h = len([1 for x in range(r) if random.randint(1,1000) % 2]) t = r - h print('There were %d rolls for head and %d rolls for tail.' % (h, t)) === Little more pythonic way... - -- शंतनू -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (Darwin) iQEcBAEBAgAGBQJO4EHrAAoJEJYe6XdHCE0VlvQH/3hhvv4PL75EQe6wyVlUOuSw fJiH8IMqm4BGUsgLOUhGyRETlDD3poI3Ae02zJN0K2VpGrNsqRq4VdN9GNBfHtoC C90eM1dCvyNHWic4Lbo+9UQGDUFXAngeDNnpY7LjNEvjpyVGsog2ptiyIauoY8zy ySun1rvcv+uUChd6+P/IEBppX6cf9ijgWt/zl8B0aCsJleCtOuh4WvTY4yX4upss hQaCDta0L6Tog1SF9PdevJgFENTVK36DEXU43xzqlEYyNLGUBTXUd2DeeKes8GJd kR553QTizOYDwWRgA7ZiJ99zr+oOt1s0wkpb01A8geXXDJo6sCfxbafAmizY18w= =uCdb -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor