Given a list of strings, where each string represents information about a person in the format “Name, Age”. You are required to count the number of senior citizens in the list. A senior citizen is defined as a person whose age is 60 or above.
For the sake of this problem, we’ll make the following assumptions:
def count_senior_citizens(info_list):
senior_citizen_count = 0
for info in info_list:
# Split the string by ',' to separate the name and age
name, age_str = info.split(", ")
age = int(age_str) # Convert age to integer
# Check if the age is 60 or more
if age >= 60:
senior_citizen_count += 1
return senior_citizen_count
# Example usage:
info_list = ["Alice, 45", "Bob, 65", "Charlie, 60", "David, 55"]
print(count_senior_citizens(info_list)) # Output: 2
This function takes a list of strings, processes each to check if the person is a senior citizen, and returns the count of senior citizens.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?