How to Create a Random Name Generator in Python
In this tutorial, we will program 'How to Create a Random Name Generator in Python'. We will learn how to generate a random name. The main goal here is to create a program that will provide a unique randomized name. I will provide a sample program to demonstrate the actual coding of this tutorial.
This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming for generating a random name. I will do my best to provide you with the easiest method of finding the actual age. So, let's start with the coding.
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- import random
- ret = False
- first_names = ['Ada', 'Bela', 'Cade', 'Dax', 'Eva', 'Fynn', 'Gia', 'Hugo', 'Ivy', 'Jax']
- last_names = ['Smith', 'Johnson', 'Williams', 'Jones', 'Brown', 'Davis', 'Miller', 'Wilson', 'Moore', 'Taylor']
- def generate_random_name():
- first_name = random.choice(first_names)
- last_name = random.choice(last_names)
- return f"{first_name} {last_name}"
- while True:
- print("\n==================== Random Name Generator ==================== \n\n")
- random_name = generate_random_name()
- input("Press Enter to generate a random name: ")
- print("\n")
- print("Random name:", random_name)
- print("\n")
- opt = input("Do you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This Python script generates random names by combining a first name randomly chosen from a list of first names and a last name randomly chosen from a list of last names. It prompts the user to press Enter to generate a random name and then displays the generated name. The user can choose to try again or exit the program.
Output:
The How to Create a Random Name Generator in Python source code that I provide can be download below. Please kindly click the download button.
There you have it we successfully created How to Create a Random Name Generator in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language
Add new comment
- 1168 views