0

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.

3
  • It sounds like you need to create a sorted list of n elements (ranging from 1 to 100), where n is the number of elements added to the sorted list. Commented May 18, 2015 at 4:06
  • You must create a function (makelist) that accept one argument, that argument will be the random number you generated that is between (5, 13). The definition of makelist should be one of a function that accept one argument, currently yours accept no arguments. Commented May 18, 2015 at 4:07
  • So really you just want an answer to part 4? Commented May 18, 2015 at 4:25

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

This worked for me, but how do I get the numbers from the loop to output on one line? and I had to add a ( to your print command, I have noticed a lot of people don't use the ( around print, is there a reason for that? BEcause when I try it without the ( it never works.
the comma after the print statement adds a space instead of a line break.. Python 2.7.6 on Yosemite
In Python3 print is a function and not a keyword statement, so it needs a parenthesis. To get the same in Python 2.7 you need a from __future__ import print_function at the top of the file.
fastest gun in the west
1
import random

def main():
    # 1 generate random number
    number = random.randint(6, 12)
    # 2 call makelist
    lst = makelist(number)
    # 6 sort return value from makelist
    lst.sort()
    # 7 print values seperated by a space using for loop
    for x in lst:
        # for python 2
        #print '%d ' % x,
        print ('%d ' % x, end="")
    print('')

def makelist(c):
    # 3 create empty list
    lst = []
    # 4 use loop to append
    for i in range(c):
        lst.append(random.randint(1, 100))
    # 5 return value
    return lst

main()

You said you were having trouble with part 4.

Your aim is to return a list of numbers. So what you will do is begin with an empty list

lst = []

and loop appending a value to that list each time. c is the value you passed into makelist and should be used as the number of times you iterate the loop

for i in range(c):

This will iterate c times (check python docs for range explanation). In each iteration append a random integer.

lst.append(random.randint(1, 100))

Your instructions were to use a loop, but the same thing can be achieved shorthand with a list comprehension.

lst = [random.randint(1, 100) for _ in range(c)]

You can use the _ variable name to indicate you don't care about value.

Also

lst = random.sample(range(1, 100), c)

Is a neater way to generate the list of random numbers, but there were clear instructions to use a loop.

Comments

0
import random

def makelist(count):
    results = []
    for i in range(0, count):
        results.append(random.randint(1, 100))
    return results

if __name__ == '__main__':
    number = random.randint(6, 12)
    print(number)
    results = makelist(number)
    results = sorted(results)
    result_line = ''
    for r in results:
        result_line = result_line + '%s ' % r
    print(result_line)

There are fancier ways of doing this, but it should be about what you're looking for!

7 Comments

Thank you for the quick response, I am having trouble trying to get it to produce anything though. When I run it nothing gets outputted...I tried to call main/makelist at the end but got a few errors in each one...what am I doing wrong? thanks again!
Try it again and see. The way it was originally written (to piggyback off your example) what not set up to be run as a script. Now it should produce output.
Still not getting anything =/. I might be doing something wrong, I am still trying to comprehend all this stuff.
And I didn't import random, but I fixed that now too.
And I was testing for main wrong (need to use ==, not is), and I had a typo... :( Long night.
|
0

This will do it:

import random

def main():
    number = random.randint(6, 12)
    print 'the number is  {0}'.format(number)
    number_list = makelist(number) # return a list of "number" ints
    sorted_list = sorted(number_list) # sort the list

    output_string = str(sorted_list[0])
    for i in range(1, number - 1):
        concat = " {0}".format(str(sorted_list[i]))
        output_string += concat # create the output string
    print output_string

def makelist(number):
    empty_list = []
    for i in range(0, number): #create a list of "number" ints
        rand_number = random.randint(1, 100)
        empty_list.append(rand_number)
    return empty_list


if __name__ == "__main__":
        main()

This returns:

the number is 11 21 22 26 31 33 35 50 71 75 95

1 Comment

OK but this will not print correctly (the problem said to print on one line separated by a space). You need something like print " ".join([str(x) for x in sorted_list]) instead of the for loop and print statement you have.
-1

Simple Python 3.6 solution:

str_Key           = ""
str_FullKey       = "" 
str_CharacterPool = "01234ABCDEFfghij~-)"
for int_I in range(64): 
    str_Key = random.choice(str_CharacterPool) 
    str_FullKey = str_FullKey + str_Key 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.