Returning a List to a Function -


import random  def genlist1():      list1 = [x x in range(100)]      count = 0      count in list1:         print(random.randint(2,25), end = " ")      return  def main():     print(genlist1())  main() 

the above code- prints 100 random digits between 2 , 25, returns "none" function. how can return list of 100 random digits function in following code.

import random  def genlist1():      list1 = [x x in range(100)]     count = 0     count in list1:         random.randint(2,25)     return list1  def genlist2():     list1 = genlist1()     list2 = [x**2 x in list1 if x % 2 == 0]     return list2  def genlist3():     list2 = genlist2()     list3 = [0.5*x x in list2]     return list3  def gensum1_2():     list2 = genlist2()     list3 = genlist3()      sum1 = 0     number1 = 0     number1 in list2:         sum1 = sum1 + number1      sum2 = 0     number2 = 0     number2 in list3:         sum2 = sum2 + number2      return sum1, sum2  def main():     print("here list 1, contains 100 random digits between 2 , 25: ")     print(genlist1())     print(), print()     print("here list 2, cubes of digits in list 1: ")     print(genlist2())     print(), print()     print("here list 3, digits in list 2 divided in half: ")     print(genlist3())     print(), print()     print("here sum of digits in list 2, , digits in list 3: ")     print(gensum1_2())  main() 

i have tried few different alterations such adding return in front of print(random....) tried assigning print(random.....) variable returning variable...with no avail.

import random  def getrandlist():     return [random.randint(2,25) x in xrange(100)]  print getrandlist() 

or, if want behave more original code...

import random  def genlist1():     randlist = [random.randint(2,25) x in xrange(100)]     elem in randlist:         print(elem, end = ' ')     return randlist  def main():     print(genlist1())  main() 

edit (to account question in comments below):

if want reuse random numbers in list, you've got few options.

option 1: best option

pass list in argument subsequent functions.

def genlist2(input_list):     list2 = [x**2 x in input_list if x % 2 == 0]     return list2  def main():     list1 = genlist1()     list2 = genlist2(list1) 

option 2: bad programming practice

if want developer other people on team hate, can things lot:

def genlist1():     global randlist     randlist = [random.randint(2,25) x in xrange(100)]     elem in randlist:         print(elem, end = ' ')     return randlist  def genlist2():     list2 = [x**2 x in randlist if x % 2 == 0]     return list2  def main():     list1 = genlist1()     list2 = genlist2() 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -