[Tutor] coin toss program without using the for/range loop

Kent Johnson kent37 at tds.net
Fri Jan 5 01:12:05 CET 2007


David wrote:
> How to write the coin toss program without using the for/range loop program.
> 
> Program flips a coin 100 times and then tells you the number of heads 
> and tails.
> 
>  
> 
> I can only solve it using the for/range loop
> 
>  
> 
> Import random
> 
>   Heads=0
> 
>   For 1 in range (100):
> 
>   Heads+=random.randrange(2)
> 
>  
> 
> print “Hit heads”+” “+str(heads)+” “+”times”+” “ + “hit tails” + “ 
> “+str(100-heads)+” “ + “times”
> 
>  
> 
> I don’t see how you solve this program just using the while loop program 
> and if/else statement.

This sounds a lot like homework so I won't give you the whole answer, 
but you can write a for loop using while and a counter variable.

Kent



More information about the Tutor mailing list