Implementing User Input for Percentile Calculation in Python

Create a Python script that prompts a user to input a percentile and outputs the corresponding age using np.percentile.

Let’s give you a little challenge. Take this age group and execute the block as shown.

Take on a practical Python coding challenge aimed at calculating age percentiles from user inputs. Learn how to utilize Python's input and percentile functions to analyze age data effectively.

Key Insights

  • Implement Python's built-in input function to prompt users to enter a percentile, converting the input string into a numeric value compatible with np.percentile.
  • Create an interactive program that calculates and clearly communicates age percentiles; for instance, inputting "90" would return "90% of all people are less than 61."
  • Practice coding skills within a Python environment, such as Noble Desktop's notebook setup, enabling users to efficiently analyze random age samples through percentile calculations.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

ML Project Workflow

  1. Define the Problem

    What outcome are you predicting and why?

  2. Prepare the Data

    Clean, normalize, encode categoricals, split into train/test.

  3. Train Models

    Start simple — logistic regression baselines often surprise.

  4. Evaluate & Iterate

    Confusion matrix, ROC, F1 — pick metrics that match the problem.

Let’s give you a little challenge. Take this age group and execute the block as shown. Now `ages` is available in our Python environment.

This is a random sample of ages. Using that sample, you’ll prompt the user for input. You can use Python’s built-in `input()` function to ask the user for some information.

Of course, we are the user in this case, since we’re running the notebook. But if someone else were reviewing it, they could also interact with it as a user, without being the programmer. We’re going to create an input box. The user will enter a percentile—such as 25,75, or any number—and get back the age below which that percentage of people fall.

If someone enters 90, you should print a string like: “90% of all people are less than 61.” You could also phrase it as “younger than 61, ” if you prefer.

For input 40, you’d display: “40% of all people are less than 27.”

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Here’s a hint: user input from `input()` always comes in as a string, and you’ll need to convert it to a number before passing it to `np.percentile()`.

All right—I’ll let you folks take a stab at that, and we’ll look at the solution in a moment.

Learn more in these courses

How to Learn Machine Learning

Master machine learning with hands-on training. Use Python to make, Modify, and Test Your Own Machine Learning Models.

Yelp Facebook LinkedIn YouTube Twitter Instagram