Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99193 > unrolled thread
| Started by | Dylan Riley <dylan.riley@hotmail.com> |
|---|---|
| First post | 2015-11-20 19:30 -0800 |
| Last post | 2015-11-22 13:09 -0500 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Dylan Riley <dylan.riley@hotmail.com> |
|---|---|
| Date | 2015-11-20 19:30 -0800 |
| Subject | anyone tell me why my program will not run? |
| Message-ID | <1737402a-2f4d-440a-abd7-6cc500f673e1@googlegroups.com> |
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")
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-11-20 22:39 -0500 |
| Message-ID | <mailman.27.1448077165.2291.python-list@python.org> |
| In reply to | #99193 |
On Fri, Nov 20, 2015 at 10:30 PM, Dylan Riley <dylan.riley@hotmail.com>
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
>
> shouldn't this below be in the while loop?
> result = random.randint(heads, tails)
> if result = heads:
> headscount += 1
> else:
> tailscount += 1
>
>
> up to here
> print(headscount, tailscount)
>
> input("press enter to exit")
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-11-21 16:50 +1100 |
| Message-ID | <mailman.28.1448085048.2291.python-list@python.org> |
| In reply to | #99193 |
On Sat, Nov 21, 2015 at 2:30 PM, Dylan Riley <dylan.riley@hotmail.com> wrote: > I have done so coming up with this piece of work but it doesnt run can anyone help me out? > Please, please, try to help us out here! What does it mean to "not run"? Does it run and give an exception? Does it run to completion but give no output? Do you type "python somefile.py" and get back an error from the system? What? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Pavel Volkov <sailor@lists.xtsubasa.org> |
|---|---|
| Date | 2015-11-21 19:57 +0300 |
| Message-ID | <mailman.42.1448125530.2291.python-list@python.org> |
| In reply to | #99193 |
On суббота, 21 ноября 2015 г. 6:30:02 MSK, 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. > First, you have a syntax error: if result = heads: should be: if result == heads: Second, there's incorrent indentation, your while loop is almost empty (not syntax error though).
[toc] | [prev] | [next] | [standalone]
| From | Pavel Volkov <sailor@lists.xtsubasa.org> |
|---|---|
| Date | 2015-11-21 20:15 +0300 |
| Message-ID | <mailman.43.1448126112.2291.python-list@python.org> |
| In reply to | #99193 |
On суббота, 21 ноября 2015 г. 6:30:02 MSK, Dylan Riley wrote:
Also some more notes:
> heads = int("1")
> tails = int("2")
Why use this strange initialization? The usual way:
heads = 1
tails = 2
gives the same result.
> while flips != 0:
> flips -= 1
There's no need to use while and flips variable:
for _ in range(100):
if random.randint(heads, tails) == heads:
headscount += 1
else:
tailscount += 1
Also, it's good to put import at the beginning.
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2015-11-21 18:44 -0800 |
| Message-ID | <dr2dnegom5afsczLnZ2dnUU7-bmdnZ2d@giganews.com> |
| In reply to | #99193 |
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 -=-
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2015-11-22 13:42 -0800 |
| Message-ID | <PK2dnbXexawgq8_LnZ2dnUU7-dmdnZ2d@giganews.com> |
| In reply to | #99226 |
On 11/21/2015 06:44 PM, Larry Hudson wrote:
> 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.
>>
[snip]
<original code>
>> 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)
[snip]
</original code>
> 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 -=-
>
I purposely didn't give any explanation of this code in my original message because I wanted to
allow people (particularly the OP) a chance to figure it out by themselves. But here's a bit of
explanation...
The counts variable is a two-element list. It's usage is, the count of heads is counts[0] and
the count of tails is counts[1] -- or equivalently, counts[HEADS] and counts[TAILS]. Both values
are initialized to 0.
The body of the for loop is the very terse (and confusing?) expression:
counts[randint(0, 1)] += 1
But if you break it down and look at the pieces individually, it's not too hard to understand.
1. randint(0, 1) gives a random value of either 0 or 1.
2. counts[...] gives you access to the heads count or tails count (... is the 0 or 1 from the
randint() function).
3. counts[...] += 1 increments the appropriate counter value.
Broken down that way it’s not too hard to understand, is it? :-)
-=- Larry -=-
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-11-22 16:39 +0000 |
| Message-ID | <n2sr3c$50c$1@reader1.panix.com> |
| In reply to | #99193 |
In <1737402a-2f4d-440a-abd7-6cc500f673e1@googlegroups.com> Dylan Riley <dylan.riley@hotmail.com> writes:
> heads = int("1")
Why are you taking the int value of a string constant? If you know you
want the value 1, why not just use it directly?
> flips = 100
> headscount = 0
> tailscount = 0
> while flips != 0:
> flips -= 1
This loop has no other effect than to set flips to zero, so why not just
set flips to zero in the first place?
> result = random.randint(heads, tails)
> if result = heads:
> headscount += 1
> else:
> tailscount += 1
Perhaps you meant to have this piece of code indented under the while
loop above?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-11-22 13:09 -0500 |
| Message-ID | <mailman.53.1448215776.2291.python-list@python.org> |
| In reply to | #99193 |
On Sat, 21 Nov 2015 20:15:05 +0300, Pavel Volkov
<sailor@lists.xtsubasa.org> declaimed the following:
>On ???????, 21 ?????? 2015 ?. 6:30:02 MSK, Dylan Riley wrote:
>
>Also some more notes:
>
>> heads = int("1")
>> tails = int("2")
>
>Why use this strange initialization? The usual way:
>heads = 1
>tails = 2
>gives the same result.
>
>> while flips != 0:
>> flips -= 1
>
>There's no need to use while and flips variable:
>
>for _ in range(100):
> if random.randint(heads, tails) == heads:
> headscount += 1
> else:
> tailscount += 1
>
>Also, it's good to put import at the beginning.
There's also no need for both headscount and tailscount unless the code
allows for a "coin" to land on its edge.
100 - headscount => tailscount
>>> TRIALS = 100
>>> import random
>>> run = [random.choice([0, 1]) for _ in range(TRIALS)]
>>> print "Heads: %s, Tails: %s" % (sum(run), TRIALS - sum(run))
Heads: 51, Tails: 49
>>>
>>> TRIALS = 200
>>> run = [random.choice([0, 1]) for _ in range(TRIALS)]
>>> print "Heads: %s, Tails: %s" % (sum(run), TRIALS - sum(run))
Heads: 112, Tails: 88
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web