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.

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.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Let's give you a little challenge. Take this age group, and you should execute this block, like so. Now ages is in our Python environment.

This is a group of a random sample of ages. In that sample, we'd like you to take input from the user. You can use the input function built into Python to prompt the user for some information.

Of course, we are the user, since we are running this notebook, but if somebody else were using this notebook taking a look at it, they could use it as a user who wasn't the programmer. But we are going to make an input box. The user is going to enter a percentile like 25 or 75 or any number and get back the age at which that percent of people are younger.

If somebody enters 90, you should print this string. 90% of all people are less than 61. You could say younger than 61, maybe.

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.

And for the input 40, you'd say 40% of all people are less than 27. A little hint. User input from the input function always comes in as a string, and we need to pass a number 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.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

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