Importing an output from another function

Magnus Lycka lycka at carmen.se
Mon Mar 20 12:03:08 CET 2006


Byte wrote:
> Now what do I do if Func1() has multiple outputs and Func2() requires
> them all to give its own output, as follows:
> 
> import random
> 
> def Func1():
>     choice = ('A', 'B', 'C')
>     output = random.choice(choice)
>     output2 = random.choice(choice)
>     return output
>     return output2
> 
> def Func2(item1, item2):
>     print item1, item2
> 
> output1 = Func1()
> Func2(output1)

Some more options (untested):

def func1(n, choice=('A', 'B', 'C')):
     # n=number of choices
     # choice can now be overridden with
     # other values
     choices = []
     for i in range(n):
         choices.append(random.choice(choice))
     return choices

def func2(choices):
     for choice in choices:
         print choice,
     print

func2(func1(2))

#########################################

class ChoosePrinter(object):
     def __init__(self, to_choose_from=('A', 'B', 'C')):
         self.to_choose_from=to_choose_from
         self.choosen = []
     def get_choices(self, n=2):
         for i in range(n):
             self.choosen.append(random.choice(choice))
     def dump_choosen(self):
         print " ".join(self.choosen)
         self.choosen = []

cp = ChoosePrinter()
cp.get_choices(2)
cp.dump_choosen()



More information about the Python-list mailing list