[Tutor] random.choice()

Dick Moores rdm at rcblue.com
Tue Jul 1 14:38:36 CEST 2008


I'm writing a demonstration version of a program which does things 
with integers, and with floats, which are randomly generated. I want 
to also randomly pick whether the integer side, or the float side is 
demonstrated next. I have 2 functions, use_for_integer_demo() 
and  use_for_float_demo(), and one or the other of these will start 
the demo. So I want to randomly choose between them. I thought that I 
might be able to use choice() to do that. So,

      (bunch of functions here)
if __name__ == '__main__':
     choice([use_for_float_demo(), use_for_integer_demo()])	

I find that what's gets run each time is BOTH of these functions, 
first use_for_float_demo(), and then use_for_integer_demo()!  What's going on?

I've used choice in simpler ways before, such as   coin = 
choice(["Heads", "Tails"]) , which is very handy. And even for a 
radically biased coin,

coin = choice(["Heads", "Tails", "Heads", "Tails", "Heads", "Tails", "Tails"])

Thanks,

Dick Moores



More information about the Tutor mailing list