[Tutor] while/if/elif/else loops

rio vagemulo at yahoo.es
Wed Nov 2 14:18:14 CET 2005


i too am a newbie to programming (except BASIC over 20 years ago!).
randrange() didnt seem to be in the random module (pyth2.4.2) 
random() IS but it doesn;t seem to take arguments. it returns a float
between 0.0 and 0.9999, so random.random()*2 means half will be
(0.0-0.99999) & half (1.0-1.99999). 
int() rounds it towards 0.
i bet it coulda been simpler or more elegant... but i think i more or less
got it working...
-------------------begin code----------------------
#Coin Toss Game

print "This game will simulate 100 coin tosses and then tell you the
number of heads and tails"

import random

tosses = 0
heads = 0
tails = 0

while tosses < 100:
    coin = int(random.random()*2)    
    print coin, # prints 0s or 1s instead of heads & tails (to save space)
    tosses +=1
    if coin == 0:
        heads +=1
    else:
        tails +=1
print
print " heads total:", heads
print " tails total:", tails
--------------------end code-----------------------
can i gush? I LOOOOOOVE python! what a great language! IDLE is awesome!
this list is awesome! i learn so much every day...

brian

zmanji at gmail wrote:
> I'm new to programming and python. I've have recently been experimenting
> with while/if/elif/else loops and I've created a simple number guessing
> game. Now I want to create a coin tossing game, but I don't know how to
> structure it in python. The code I already have (but it's not working)
> is below.
> 
> 
> #Coin Toss Game
> 
> print "This game will simulate 100 coin tosses and then tell you the
> number of head's and tails"
> 
> import random
> 
> tosses = 0
> heads = 0
> tails = 0
> 
> while tosses = 100<0:
>    coin = randrange(1)
>    tosses +=1
>    if coin == 0:
>       heads +=1
>       print "Heads"
>     else:
>       tails +=1
>       Print "Tails"



	

	
		
__________________________________________________________ 
Find your next car at http://autos.yahoo.ca


More information about the Tutor mailing list