Python random number generation
Question: This is the question I am trying to answer:

  1. In main, generate a random integer that is greater than 5 and less than 13. print this number on its own line. - **Done**
  2. Call the makelist function with the random integer as sole argument. - **This confuses me**
  3. Make an empty list inside the makelist function. - **Done**
  4. Use a loop to append to the list a number of elements equal to the random integer argument. All new list elements must be random integers ranging from 1 to 100, inclusive. Duplicates are okay. - **Not Done**
  5. Return the list to main. - **Not Done**
  6. Back in main, catch the returned list and sort it. - **Not Done**
  7. Finally, use a for loop to display the sorted list elements, all on one line, separated by single spaces. - **Not Done**

This is what I have so far:

    
    
    import random
    
    def main():
        number = random.randint(6, 12)
    
    print('the number is', number)
    
    
    def makelist():
        numbers = random.randint(1, 100)
        empty_list = []
        empty_list.append(numbers)
    

I am having trouble trying to understand the loops/append part...could someone
give me some guidance? Thanks.


Answer: This works.

    
    
    #!/usr/bin/python
    import sys, random
    
    def makelist(number):
      new_list = []
      for i in range(0, number):
        new_rand = random.randint(1, 100)
        new_list.append(new_rand)
      return new_list
    
    def main():
      number = random.randint(6, 12)
      print "the number is %s" % str(number)
      populated_list = makelist(number)
      populated_list.sort()
      for i in populated_list:
        print(str(i)),
    
    main()
    

Output:

test 1

    
    
    the number is 11
    9 11 13 17 25 33 53 61 65 85 87
    

test 2

    
    
    the number is 8
    1 14 17 23 32 49 51 81
    

test 3

    
    
    the number is 10
    16 18 24 29 35 46 50 74 78 88
    

