Given a list of integers age = [14, 19, 20, 15, 16, 40, 39], I need to determine which percentage of people are not in high school (High school: Age 14-18). I created if conditions to determine whether the people are in high school, but I don't know how to determine the % of people not in high school after.
age = [14, 19, 20, 15, 16, 40, 39]
for i in age:
if (i>=14 and i<=18):
print("In high school")
else:
print("Not in high school")
Any help is appreciated. Thanks!
CodePudding user response:
You can simply sum up the generator function to get the count of people in high school.
age = [14, 19, 20, 15, 16, 40, 39]
count = sum(1 for i in age if 14 <= i <= 18)
percent = (count/len(age))*100
CodePudding user response:
I would start by counting the number of people you find who are in high school and then dividing by the total amount of people.
( Num people in high school / Num total people ) * 100 = % of people in high school.
CodePudding user response:
Use Generator expression like this:
import math
age = [14, 19, 20, 15, 16, 40, 39]
percent = math.floor(sum(1 for i in age if i >= 14 and i <= 18)/len(age) * 100)
print(percent) # 42