program7.py This program requires the main function and a custom value-returning function. In the main function, code these steps in this sequence: use a list comprehension to generate 100 random integers all from 1 to 100, inclusive. These represent the ages of a population of 100 people. pass the list as the sole argument to the custom value-returning function. In the custom function: sort the list in descending order using methods described in Chapter 7 return the sorted list to main Back in main: Within a for loop, print a 10 by 10 representation of the ages returned by the custom function. As listed in the example output, the ages should be in descending order and evenly spaced. Also, within the same for loop accumulate the sum of all ages. Using a built-in function determine the age of the oldest person, then print it . Using a built-in function to determine the age of the youngest person, then print it . Determine the average age by making use of the sum of all ages calculated in the for loop above and a built-in function, then print the average age so that two decimal positions are included. Within a for loop, count the minor ages less than 18 count the senior ages that are greater than 65 Print the count of minor ages and senior ages . Use either a loop or a list comprehension to create a list of college ages (ages that are greater or equal to 18 and less than or equal to 21). Print the count of senior ages making use of a built-in function. My code so far is: # importing random module import random # main function definition def main(): # declared empty list, arrList arrList = [] # append 50 random numbers in arrList # with the value ranges from 1 and 100 inclusive for i in range(0,100): # randomNum stores the random number between 1 and 100 inclusive # by using random.randint(1,100) function randomNum = random.randint(1,100) # append randomNum to the arrList arrList.append(randomNum) # sort the arrList in ascending order arrList.sort() # print the elements of arrList in one line with spaces print("\nElements in the list: ") for i in range(0,len(arrList)): # printing elements print(arrList[i],end=" ") main() Additonal help with explanations would be great, thank you in advance!
program7.py
This program requires the main function and a custom value-returning function.
In the main function, code these steps in this sequence:
- use a list comprehension to generate 100 random integers all from 1 to 100, inclusive. These represent the ages of a population of 100 people.
- pass the list as the sole argument to the custom value-returning function.
In the custom function:
- sort the list in descending order using methods described in Chapter 7
- return the sorted list to main
Back in main:
- Within a for loop, print a 10 by 10 representation of the ages returned by the custom function. As listed in the example output, the ages should be in descending order and evenly spaced. Also, within the same for loop accumulate the sum of all ages.
- Using a built-in function determine the age of the oldest person, then print it .
- Using a built-in function to determine the age of the youngest person, then print it .
- Determine the average age by making use of the sum of all ages calculated in the for loop above and a built-in function, then print the average age so that two decimal positions are included.
- Within a for loop,
- count the minor ages less than 18
- count the senior ages that are greater than 65
- Print the count of minor ages and senior ages .
- Use either a loop or a list comprehension to create a list of college ages (ages that are greater or equal to 18 and less than or equal to 21).
- Print the count of senior ages making use of a built-in function.
My code so far is:
# importing random module
import random
# main function definition
def main():
# declared empty list, arrList
arrList = []
# append 50 random numbers in arrList
# with the value ranges from 1 and 100 inclusive
for i in range(0,100):
# randomNum stores the random number between 1 and 100 inclusive
# by using random.randint(1,100) function
randomNum = random.randint(1,100)
# append randomNum to the arrList
arrList.append(randomNum)
# sort the arrList in ascending order
arrList.sort()
# print the elements of arrList in one line with spaces
print("\nElements in the list: ")
for i in range(0,len(arrList)):
# printing elements
print(arrList[i],end=" ")
main()
Additonal help with explanations would be great, thank you in advance!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images