Home>

] (C5c2f26002440f8b9f5426e91f441999.png)

The return value is the function of the function using a list: a random number in the range of integers given by the first two arguments, the number of values ​​in the third argument.

The process of generating and storing it in a list and returning it as a return value does not work.
It says num is not defind and I tried to define num but it doesn't work so please give me a hint alone
Thank you.

import random
def gen_ransu (start, end, num):
    ransu.append (random.randit (start, end))
for i in range (gen_ransu (num)):
    gen_ransu (start, end)

sample1 = gen_ransu (0,10,5)
print (sample1)
sample2 = gen_ransu (10,20,10)
print (sample2)
sample3 = gen_ransu (100,200,15)
print (sample3)
  • Answer # 1

    There are many areas that need to be corrected, not just the part you asked. Please review it with reference to the comments below.

    import random
    def gen_ransu (start, end, num):
            ransu = [] #Append list is initialized with [] first
            #Please indent the processing in the function
            for i in range (num): Used in the form #range (int)
                    ransu.append (random.randint (start, end)) # randint instead of randit
            return gen_ransu # Don't forget the return value of the function
    sample1 = gen_ransu (0,10,5)
    print (sample1)

  • Answer # 2

    You can probably do what you want with the code below.

    import random
    def gen_ransu (start, end, num):
      ransu = []
      for _ in range (num):
        ransu.append (random.randint (start, end))
      return ransu
    sample1 = gen_ransu (0,10,5)
    print (sample1)
    sample2 = gen_ransu (10,20,10)
    print (sample2)
    sample3 = gen_ransu (100,200,15)
    print (sample3)