[Tutor] Need help solving this problem
Albert-jan Roskam
fomcl at yahoo.com
Wed Jun 10 22:38:34 CEST 2009
hi!
This is how I would do it, but I'm still learning this too, so I'm very much open for suggestions.
Cheers!!
Albert-Jan
import random
def draw ():
return random.sample(["head", "tail"], 1)
def toss ():
heads, tails = 0, 0
for flip in range(100):
if draw() == ["head"]: heads += 1
else: tails += 1
return heads, tails
for attempt in range(20):
print "attempt:", attempt+1, "heads:", toss()[0], "tails:", toss()[1]
--- On Wed, 6/10/09, Raj Medhekar <cosmicsand27 at yahoo.com> wrote:
> From: Raj Medhekar <cosmicsand27 at yahoo.com>
> Subject: [Tutor] Need help solving this problem
> To: "Python Tutor" <tutor at python.org>
> Date: Wednesday, June 10, 2009, 10:08 PM
> I
> have been teaching myself Python using a book. The chapter I
> am on currently, covers branching, while loops and program
> planning. I am stuck on on of the challenges at the end of
> this chapter, and I was hoping to get some help with this.
> Here it is:
>
> Write a program that flips a coin 100 times and the tells
> you the number of heads and tails.
>
> I have tried to think about several ways to go about doing
> this but I have hit a wall. Even though I understand the
> general concept of branching and looping, I have been having
> trouble writing a program with these. I look forward
> to your reply that will help me understand these structures
> better.
>
> Sincerely,
> Raj
>
>
>
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list