Python Random Name Picker Example
A simple Python project can help you practice lists, user input, and basic randomness.
In this example, you will build a script that picks one random name from a list. This page focuses on how the script works and how to build it step by step, not on covering the full random module.
Quick example
import random
names = ["Alice", "Bob", "Carlos", "Dina"]
chosen_name = random.choice(names)
print("Selected:", chosen_name)
Use random.choice() to return one item from a list. The list must not be empty.
What this example does
This script:
- Creates a list of names
- Uses the
randommodule - Selects one name at random
- Prints the result
This is a useful beginner project because it combines a few core Python ideas in one short program.
Basic random name picker script
Start with a fixed list of names.
import random
names = ["Alice", "Bob", "Carlos", "Dina"]
chosen_name = random.choice(names)
print("Selected:", chosen_name)
Expected output
Your output will change each time you run the program. For example:
Selected: Carlos
or:
Selected: Alice
Why this works
import randommakes therandommodule availablenamesis a list containing several stringsrandom.choice(names)picks one item from that list- The result is stored in
chosen_name print()shows the selected name
If you are new to lists, see Python lists explained for beginners.
How the code works step by step
Let's break the script into small parts.
1. Import the random module
import random
random is a built-in Python module used for random operations.
You need to import it before calling random.choice().
If you want a broader explanation later, see the Python random module overview.
2. Create a list of names
names = ["Alice", "Bob", "Carlos", "Dina"]
A list lets you store multiple values in one variable.
Here, each item in the list is a name stored as a string.
3. Choose one random item
chosen_name = random.choice(names)
random.choice(list_name) returns one random item from the list.
If you want to understand this function more clearly, see random.choice() explained.
4. Print the result
print("Selected:", chosen_name)
This displays the randomly selected name.
Example with user input
Instead of hard-coding the names, you can let the user type them.
The user will enter names separated by commas, such as:
Alice, Bob, Carlos, Dina
Then the program will:
- Read the text
- Split it into a list
- Remove extra spaces
- Pick one random name
import random
user_input = input("Enter names separated by commas: ")
names = user_input.split(",")
cleaned_names = [name.strip() for name in names]
chosen_name = random.choice(cleaned_names)
print("Selected:", chosen_name)
How this version works
input()reads text from the usersplit(",")turns one string into a list using commasstrip()removes extra spaces around each namerandom.choice()picks one cleaned name
If you need help with these parts, see:
Handle empty input safely
An empty list will cause an error with random.choice().
For example, this is unsafe if the user enters nothing useful:
import random
user_input = input("Enter names separated by commas: ")
names = [name.strip() for name in user_input.split(",")]
chosen_name = random.choice(names)
print("Selected:", chosen_name)
A safer version checks that the list actually contains names.
import random
user_input = input("Enter names separated by commas: ")
names = [name.strip() for name in user_input.split(",") if name.strip()]
if names:
chosen_name = random.choice(names)
print("Selected:", chosen_name)
else:
print("No names were provided.")
Why this version is better
if name.strip()removes blank entriesif names:checks that the list is not empty- The program prints a helpful message instead of failing
This is especially useful when the user enters:
- an empty string
- only spaces
- commas with no real names
Useful beginner improvements
Once the basic version works, try small changes.
Pick more than one name
If you want multiple different names without duplicates, use random.sample().
import random
names = ["Alice", "Bob", "Carlos", "Dina"]
chosen_names = random.sample(names, 2)
print("Selected:", chosen_names)
This picks 2 different names from the list.
Prevent duplicate blank entries
If the user types messy input, you can clean it more carefully:
user_input = input("Enter names separated by commas: ")
names = [name.strip() for name in user_input.split(",") if name.strip()]
print(names)
This removes empty items like "".
Repeat the selection in a loop
You can let the program choose again and again:
import random
names = ["Alice", "Bob", "Carlos", "Dina"]
while True:
print("Selected:", random.choice(names))
again = input("Pick another name? (yes/no): ").strip().lower()
if again != "yes":
break
Use it in real situations
This small script can be used for:
- classroom activities
- choosing a team member
- picking a winner from a short list
- simple games and practice projects
Common mistakes
Here are some common problems beginners run into.
Forgetting to import random
This will fail:
names = ["Alice", "Bob", "Carlos"]
print(random.choice(names))
Fix it by adding:
import random
Using random.choice() on an empty list
This causes an error because there is nothing to choose.
Bad example:
import random
names = []
print(random.choice(names))
Fix it by checking the list first:
if names:
print(random.choice(names))
else:
print("The list is empty.")
Reading input but not splitting it
This is wrong if you expect multiple names:
user_input = input("Enter names: ")
names = user_input
Here, names is still just one string.
Fix it with:
names = user_input.split(",")
Keeping extra spaces around names
If the input is:
Alice, Bob, Carlos
then splitting alone may produce values with spaces, such as " Bob".
Fix it with strip():
names = [name.strip() for name in user_input.split(",")]
Misspelling choice
This is incorrect:
random.choise(names)
The correct function name is:
random.choice(names)
Helpful debugging checks
If your program is not working, print values to inspect them:
print(names)
print(type(names))
print(len(names))
print(repr(user_input))
These checks help you answer questions like:
- Is
namesreally a list? - Is the list empty?
- Did the input contain extra spaces?
- Did the program read what you expected?
FAQ
How do I pick a random name from a list in Python?
Use random.choice(your_list) after importing the random module.
Why does random.choice() fail?
It usually fails because the list is empty or the random module was not imported correctly.
Can I let the user enter the names?
Yes. Read input as text, split it by commas, clean spaces, and then choose from the resulting list.
How do I pick more than one random name?
Use random.sample() if you want multiple different names without repeats.
See also
- Python random module overview
- random.choice() function explained
- Python lists explained for beginners
- How to get user input in Python
- How to split a string in Python
Try changing this project next by:
- accepting names from the user
- repeating the pick in a loop
- choosing multiple names without duplicates