Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. def main()
  2.  
  3.     # create an empty list named nums. woops :)
  4.     nums = []
  5.  
  6.     # use a loop to add 20 random integers, all in the range from 1-100, to nums.
  7.     # for this we will need a random g
  8.     import random
  9.     for i in range(20):
  10.         nums.append(random.randint(1,100))
  11.        
  12.     # print the total of all list elements.
  13.     print(nums)
  14.  
  15.     # print the highest number in the list.
  16.     print(max(nums))
  17.  
  18.     # print the lowest number in the list.
  19.     print(min(nums))
  20.  
  21.     # sort the list and then reverse it.
  22.     nums.sort(reverse=True)
  23.  
  24.     # use another loop to display the sorted numbers in descending order,
  25.     # all on the same line, separated by a single space.
  26.     my_string = ""
  27.     for number in nums:
  28.         my_string = my_string + str(number) + " "
  29.     # this part removes the extra space at the end
  30.     my_string = my_string[0:-1]
  31.  
  32.     print(my_string)
  33.  
  34.     # determine if 75 is in the list. If it is, report the index of its first occurrence.
  35.     # If it is absent from the list, indicate that, too.
  36.     if 75 in nums:
  37.         index_of_75 = nums.index(75)
  38.     else:
  39.         index_of_75 = "Not in list"
  40.         print(index_of_75)
  41.        
  42.     # make a new list named middle_10 by slicing out the middle 10 elements of the descending list.
  43.     # Use a loop to display the elements in middle_10 all on one line.
  44.     middle_10 = nums[6-1:16-1]
  45.     my_2nd_string = ""
  46.     for number in middle_10:
  47.         my_2nd_string = my_2nd_string + str(number) + " "
  48.     # this part removes the extra space at the end
  49.     my_2nd_string = my_2nd_string[0:-1]
  50.     print(my_2nd_string)
  51.  
  52.     # make an empty list named evens and another empty list named odds.
  53.     evens = []
  54.     odds = []
  55.  
  56.     # use a loop to process nums,
  57.     # adding even elements and odd elements to their respective lists.
  58.     for number in nums:
  59.         if number % 2 == 0:
  60.             # number is even
  61.             evens.append(number)
  62.         else:
  63.             # number is odd
  64.             odds.append(number)
  65.  
  66.     # print both evens and odds. Crude dumps inside [ ] as shown next are okay.
  67.     print("Evens: " + str(evens))
  68.     print("Odds: " + str(odds))
  69.  
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement