115k views
2 votes
Write a function named buildList that builds a list by appending a given number of random integers from 100 to 199 inclusive. It should accept two parameters — the first parameter is the list, and the second is an integer for how many random values to add, which should be input by the user.

Print the list after calling buildList. Sort the list and then print it again.

Sample Run
How many values to add to the list:
10
[141, 119, 122, 198, 187, 120, 134, 193, 112, 146]
[112, 119, 120, 122, 134, 141, 146, 187, 193, 198]


python

2 Answers

4 votes

Answer:

import random

def buildList(num1, num2):

for i in range(num2):

num1.append(random.randint(100, 199))

return num1

x = int(input("How many values to add to the list: "))

list = []

buildList(list , x)

print(list)

list.sort()

print(list)

Step-by-step explanation:

I don't think anyone would read the explanation anyway soooo...

Trust Me Bro

User Vaclav
by
7.6k points
4 votes

Answer:

import random

def buildList(lst, num):

for i in range(num):

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

return lst

user_input = int(input("How many values to add to the list: "))

my_list = []

print(buildList(my_list, user_input))

my_list.sort()

print(my_list)

This function takes in 2 parameters, a list, and an integer. It uses a for loop to generate a specified number of random integers between 100-199 and appends them to the list. The function then returns the list. The user is prompted to input the number of random integers they want to add to the list, this value is stored in the 'variable user_input'. Then the function is called with an empty list and the user-specified value. The resulting list is printed and then sorted and printed again.

User UJey
by
6.3k points