Reputation: 11
This is the question I am trying to answer:
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.
Upvotes: 0
Views: 5697
Reputation: 1290
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
Upvotes: -1
Reputation: 21609
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.
Upvotes: 1
Reputation: 1170
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
Upvotes: 0
Reputation: 26812
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
Upvotes: 1
Reputation: 1978
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!
Upvotes: 0