[Tutor] Giving a name to a function and calling it, rather than calling the function directly

lists lists at justuber.com
Sun Sep 5 00:39:07 CEST 2010


>>     if coinToss(1,2) == 1:
>>         heads += 1
>>         tossNo += 1
>>     else:
>>         tails += 1
>>         tossNo += 1
>
> Looking good. You can hoist "tossNo += 1" out of each branch of your if
> statement too, if you like, to make it even more streamlined (In
> other words, execute it once, right after the coin flip, and before
> the if-statement).
>
> Alan

Ah right, I get you. i.e

while tossNo <= 99:
    coinToss = random.randint
    tossNo += 1
    if coinToss(1,2) == 1:
        heads += 1
    else:
        tails += 1

That makes sense. :-)

Chris


More information about the Tutor mailing list