In Python, list of random integers

def change_list(src_list):
  # hold the middle 6 elements of the list
  dest_list = src_list[3:9] # use a slice to make a new list

  # Print the size
  print('The size of the list is now', len(dest_list))

  # sort the new list in ascending order
  dest_list.sort()
  return dest_list

def main():

  # create an empty list
  my_list = []
  from random import randrange

  # use a for loop to add 12 random integers
  for x in range(12):
    #  all ranging from 50 to 100
    my_list.append(randrange(50, 100))

  # for loop to iterate over the list and display 
  # all elements on one line separated by a single space.
  print('Here is the list of random integers...', end=' ')
  for x in my_list:
    print(x, end=' ')
  print()

  # display the 4th element
  print('The 4th element in the list is', my_list[3])

  # the element at index 9
  print('The element at index 9 is', my_list[9])

  # the smallest element
  print('The smallest element in the list is', min(my_list))

  # call the change_list function with your list as its sole argument
  new_list = change_list(my_list)

  # for loop to iterate over the list and display 
  # all elements on one line separated by a single space.
  print('change_list returned this sorted list...', end=' ')
  for x in new_list:
    print(x, end=' ')
  print()
if '__main__' == __name__:
  main()

Comments

Popular posts from this blog