Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99226

Re: anyone tell me why my program will not run?

Subject Re: anyone tell me why my program will not run?
Newsgroups comp.lang.python
References <1737402a-2f4d-440a-abd7-6cc500f673e1@googlegroups.com>
From Larry Hudson <orgnut@yahoo.com>
Date 2015-11-21 18:44 -0800
Message-ID <dr2dnegom5afsczLnZ2dnUU7-bmdnZ2d@giganews.com> (permalink)

Show all headers | View raw


On 11/20/2015 07:30 PM, Dylan Riley wrote:
> i am learning python and was tasked with making a program that flips a coin 100 times and then tells you
> the number of heads and tails.
>
> I have done so coming up with this piece of work but it doesnt run can anyone help me out?
>
> #This is credited to dylan
>
> print(" \\ \\ \\ \\ \\ \\ \\ \\ D FLIPS \\ \\ \\ \\ \\ \\ \\ \\")
> print("\n\nThis is D's coin flipper program. You get 100 flips. \n\t LETS SEE HOW LUCKY YOU ARE")
> input("Press enter")
>
> import random
>
> heads = int("1")
> tails = int("2")
> flips = 100
> headscount = 0
> tailscount = 0
>
> while flips != 0:
>      flips -= 1
>
> result = random.randint(heads, tails)
> if result = heads:
>      headscount += 1
> else:
>      tailscount += 1
>
>
> print(headscount, tailscount)
>
> input("press enter to exit")
>
It doesn't run because it if full of errors, which have already been discussed by others.

I just wanted to show you a (radically) different approach that you can study (or not... your 
choice).  I'm leaving out your heading and just showing the heart of the program.  I am not 
necessarily recommending this, I just wanted you to see a different way of looking at the 
problem.  Except for the initialization and printing of the results, the entire thing is done in 
one two-line for loop.

<code>
from random import randint

#  Put your heading text here...

HEADS = 0
TAILS = 1    #  Note:  Python _convention_ is to upper-case constants.
counts = [0, 0]

for flips in range(100):
     counts[randint(0, 1)] += 1

print('Number of heads: ', counts[HEADS])
print('Number of tails: ', counts[TAILS])
</code>

Note that the HEADS and TAILS constants are only used in one place (the final print functions), 
you could simply leave them out and directly use 0 and 1 in those final print()s.

      -=- Larry -=-

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

anyone tell me why my program will not run? Dylan Riley <dylan.riley@hotmail.com> - 2015-11-20 19:30 -0800
  Re: anyone tell me why my program will not run? Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-20 22:39 -0500
  Re: anyone tell me why my program will not run? Chris Angelico <rosuav@gmail.com> - 2015-11-21 16:50 +1100
  Re: anyone tell me why my program will not run? Pavel Volkov <sailor@lists.xtsubasa.org> - 2015-11-21 19:57 +0300
  Re: anyone tell me why my program will not run? Pavel Volkov <sailor@lists.xtsubasa.org> - 2015-11-21 20:15 +0300
  Re: anyone tell me why my program will not run? Larry Hudson <orgnut@yahoo.com> - 2015-11-21 18:44 -0800
    Re: anyone tell me why my program will not run? Larry Hudson <orgnut@yahoo.com> - 2015-11-22 13:42 -0800
  Re: anyone tell me why my program will not run? John Gordon <gordon@panix.com> - 2015-11-22 16:39 +0000
  Re: anyone tell me why my program will not run? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-22 13:09 -0500

csiph-web