[Tutor] Need help solving this problem

2009-06-10 Thread Raj Medhekar
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



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread Albert-jan Roskam

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 cosmicsan...@yahoo.com wrote:

 From: Raj Medhekar cosmicsan...@yahoo.com
 Subject: [Tutor] Need help solving this problem
 To: Python Tutor tutor@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@python.org
 http://mail.python.org/mailman/listinfo/tutor
 


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread Robert Berman
What you are looking at is a simulation whereby a coin having 2 outcomes
(heads or tails) is flipped exactly 100 times. You need to tell how many
times the coin falls heads up and how many times the coin falls tails
up. 

First become familiar with the random module. Assign a value of 1 for
heads and a value of 2 for tails. Then you are going to use a structure
of the random module which will return only two possible outcomes.
Either a one or a two. You are going to loop (look at range(1,101) or
range(0,100). (Side question; why 101 or 0 to 100?; look closely at the
meaning of the range arguments). Simply count the number of ones and the
number of two's in the 100 flips then print the results. 

Good luck.

Robert


On Wed, 2009-06-10 at 13:08 -0700, Raj Medhekar wrote:
 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
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread karma
Hi Raj,

I'm another learner, I used the following:

 def toss(n):
heads = 0
for i in range(n):
heads += random.randint(0,1)
return heads, n-heads

 print %d heads, %d tails % toss(100)

Best of luck in your python endeavors!

2009/6/10 Raj Medhekar cosmicsan...@yahoo.com:
 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


 ___
 Tutor maillist  -  tu...@python.org
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread David

Raj Medhekar wrote:
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




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

I am learning also, I came up with;

#!/usr/bin/python

import random
random = random.Random()
choice = ['heads', 'tails']
head_wins = 0
tail_wins = 0

def coin_flip():
flip = random.choice(choice)
global head_wins
global tail_wins
if flip == 'heads':
head_wins += 1
elif flip == 'tails':
tail_wins += 1

def number_flips():
for i in range(100):
coin_flip()

number_flips()
print 'Head Total Wins =', head_wins
print 'Tail Total Wins =', tail_wins


--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread Alan Gauld


Raj Medhekar cosmicsan...@yahoo.com wrote

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.


You also need to understand the concept of variables as data stores.
And you will probably need to use a mechanism to generate random
values. The random module will help or you can simply read the time
and strip off the last two digits - that will be good enough for your
purposes!

Otherwise take a look at my tutorial for the various topics
Raw Materials - variables and data
Loops
Branches
File handling - for a very brief intro to reading the time

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help solving this problem

2009-06-10 Thread ayyaz

Raj Medhekar wrote:
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




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


A very simple solution

import random # you need to import this module to generate random 			 
   # numbers


heads = 0;  #init number of heads;
tails = 0;  #init number of tails;
cnt = 0 # variable to store number of coin tosses
while cnt100:  # we want this loop to 100 times
toss = random.randrange(0,2,1) # this line will 
randomly choose 		   # between 0 and 1

if toss == 0:
tails += 1 # if the random generator gives 0, then we 
increase    # count of tail by one

else:
heads += 1 # if the random generator gives 1, then we increase
   # count of head by one   
cnt += 1   # increase count of coin toss by one

print Number of heads, heads # print the number of heads
print Number of tails, tails # print the number of tails

raw_input(\nPress enter to exit)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor