Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main()
- # create an empty list named nums. woops :)
- nums = []
- # use a loop to add 20 random integers, all in the range from 1-100, to nums.
- # for this we will need a random g
- import random
- for i in range(20):
- nums.append(random.randint(1,100))
- # print the total of all list elements.
- print(nums)
- # print the highest number in the list.
- print(max(nums))
- # print the lowest number in the list.
- print(min(nums))
- # sort the list and then reverse it.
- nums.sort(reverse=True)
- # use another loop to display the sorted numbers in descending order,
- # all on the same line, separated by a single space.
- my_string = ""
- for number in nums:
- my_string = my_string + str(number) + " "
- # this part removes the extra space at the end
- my_string = my_string[0:-1]
- print(my_string)
- # determine if 75 is in the list. If it is, report the index of its first occurrence.
- # If it is absent from the list, indicate that, too.
- if 75 in nums:
- index_of_75 = nums.index(75)
- else:
- index_of_75 = "Not in list"
- print(index_of_75)
- # make a new list named middle_10 by slicing out the middle 10 elements of the descending list.
- # Use a loop to display the elements in middle_10 all on one line.
- middle_10 = nums[6-1:16-1]
- my_2nd_string = ""
- for number in middle_10:
- my_2nd_string = my_2nd_string + str(number) + " "
- # this part removes the extra space at the end
- my_2nd_string = my_2nd_string[0:-1]
- print(my_2nd_string)
- # make an empty list named evens and another empty list named odds.
- evens = []
- odds = []
- # use a loop to process nums,
- # adding even elements and odd elements to their respective lists.
- for number in nums:
- if number % 2 == 0:
- # number is even
- evens.append(number)
- else:
- # number is odd
- odds.append(number)
- # print both evens and odds. Crude dumps inside [ ] as shown next are okay.
- print("Evens: " + str(evens))
- print("Odds: " + str(odds))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement