Part of the series

Several example codes

~2 min read · Updated Oct 6, 2025

Program Overview

This Python program reads the age and gender of multiple individuals.
It calculates and displays:
- The number of people aged between 18 and 50
- The percentage of such individuals among all entries
- The average age of those in the 18–50 range
The program ends when the user enters age = 0.


Python Code:


total_count = 0
valid_count = 0
age_sum = 0

while True:
    age = int(input("Enter age (0 to exit): "))
    if age == 0:
        break

    gender = input("Enter gender (M/m for male): ").strip()

    total_count += 1

    if 18 <= age <= 50:
        valid_count += 1
        age_sum += age

# Calculate percentage and average
if valid_count > 0:
    percentage = (valid_count / total_count) * 100
    average_age = age_sum / valid_count
else:
    percentage = 0
    average_age = 0

# Display results
print(f"\nTotal entries: {total_count}")
print(f"Count aged 18–50: {valid_count}")
print(f"Percentage aged 18–50: {percentage:.2f}%")
print(f"Average age (18–50): {average_age:.2f}")

Sample Output:


Enter age (0 to exit): 25  
Enter gender (M/m for male): m  
Enter age (0 to exit): 52  
Enter gender (M/m for male): f  
Enter age (0 to exit): 40  
Enter gender (M/m for male): M  
Enter age (0 to exit): 0  

Total entries: 3  
Count aged 18–50: 2  
Percentage aged 18–50: 66.67%  
Average age (18–50): 32.50

Step-by-Step Explanation:

- The program loops until age = 0 is entered
- Each entry updates the total count
- If age is between 18 and 50, it contributes to the valid count and age sum
- After the loop, percentage and average are calculated and displayed


Written & researched by Dr. Shahin Siami

Related Articles

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

Continue

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

Continue

Several example codes

understand the Python programming language

Continue

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

Continue

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

Continue

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

Continue