From 5051f00edf0ce9914b963f439ffd8c59b088505d Mon Sep 17 00:00:00 2001 From: scharukh7 Date: Thu, 6 Apr 2023 15:27:53 +0100 Subject: [PATCH 1/2] completed project --- README.md | 71 ++++++++++++++++++++++------------------------- name-generator.py | 43 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 name-generator.py diff --git a/README.md b/README.md index 0e4526e..68a68b6 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,33 @@ -# Beginners Python Project - Random Name Generator -Building a Random Name Generator application. -## Introduction -In this project, you will be building a text-based user interface that will take a input and output a list of random names from a pre-defined list. - -## Learning Objects -- User inputs -- Validating inputs -- Importing packages -- Documenting code -- Writing functions -- Control flows -- Printing outputs to the user -- Using random functionality in Python -- Basic structure of Python Scripts - -## Project Brief -This application with simulate randomly generating names from a pre-defined list. A use case could be: a set of new parents can't choose a name from a shortlist. Why not use Python to help! - -The application will: -- Take a user input of shortlist names. -- Take a second user input of how many names they want randomly selected from that shortlist. This should be a positive integer. The app should check if the input is valid. -- Print to the user the randomly selected names. - -## Suggestions -Feel free to structure the application however you see fit, but here is a project structure recommendation. If you have experience with pseudocode or flowcharts, I would recommend mapping out the project using these tools before you start any programming. - -I would also recommend using Visual Studio Code as your editor, but this is personal preference. Make sure you implement coding standards and Pythonic style in the application! - -Suggested Structure: -- Contain all code in one main.py file. Could also be a .ipynb file. -- A README file consisting of project documentation. I would recommend a .md file. -- Read the user's input using Python's inbuilt input() function i.e. multiple calls to input(). -- A user-defined function that validates and parses the user's input. Think of a appropriate function name and appropriate checks. -- A user-defined function that uses Python's random package to select the names from the shortlist. Think of a appropriate function name. -- Prints the shortlisted names to the user. - -## Good Luck! \ No newline at end of file +# Random Name Selector + +This Python script helps you randomly select a specified number of names from a shortlist provided by the user. It takes a user input of shortlist names separated by commas and a second user input for the number of names to be randomly selected from the shortlist. It then prints the randomly selected names. + +## Features + +- Accepts a user-defined shortlist of names +- Validates the user input for the number of names to be selected +- Randomly selects and prints the specified number of names from the shortlist + +## Getting Started + +### Prerequisites + +- Python 3 and above installed on your system + +### Running the Script +Run the python script name-generator.py + +## Usage + +1. When prompted, enter the shortlist names separated by commas: + +Enter shortlist names separated by a comma: Beckham, Ronaldo, Messi, Silva, Aguero, Reus, DeBruyne + +2. When prompted, enter the number of names you want randomly selected from the shortlist: + +Enter the number of names you want randomly selected from the shortlist: 3 + +3. The script will then print the randomly selected names: + +Randomly selected names: Ronaldo, Aguero, DeBruyne + diff --git a/name-generator.py b/name-generator.py new file mode 100644 index 0000000..78dc352 --- /dev/null +++ b/name-generator.py @@ -0,0 +1,43 @@ +import random + +# Function to randomly select a specified number of names from a list of names +def random_name_selector(names, num_selections): + # Check if the number of selections is valid (greater than 0 and less than or equal to the number of names) + if num_selections > 0 and num_selections <= len(names): + # Randomly select the specified number of names + selected_names = random.sample(names, num_selections) + return selected_names + else: + # Return an error message if the number of selections is invalid + return "Invalid input: number of selections should be between 1 and the length of the shortlist." + +# Main function to handle user input and output +def main(): + # Take user input for shortlist names + shortlist_names = input("Enter shortlist names separated by a comma: ") + # Create a list of names by splitting the input string and removing extra spaces + names_list = [name.strip() for name in shortlist_names.split(',')] + + # Take user input for the number of names to be selected + while True: + try: + num_names = int(input("Enter the number of names you want randomly selected from the shortlist: ")) + # Exit the loop when a valid positive integer is provided + break + except ValueError: + # Display an error message if the input is not a positive integer + print("Invalid input: Please enter a positive integer.") + + # Call the random_name_selector function with the list of names and the number of selections + selected_names = random_name_selector(names_list, num_names) + # Check if the function returned a list of names + if isinstance(selected_names, list): + # Print the randomly selected names if the function returned a list + print(f"Randomly selected names: {', '.join(selected_names)}") + else: + # Print the error message if the function did not return a list + print(selected_names) + +# Call the main function to run the code +if __name__ == "__main__": + main() \ No newline at end of file From 41282ab903cd67f686454b7b9da6ade94ca39f20 Mon Sep 17 00:00:00 2001 From: Shahrukh Shah <102545327+Scharukh7@users.noreply.github.com> Date: Wed, 12 Apr 2023 14:09:18 +0100 Subject: [PATCH 2/2] Delete README.md --- README.md | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 68a68b6..0000000 --- a/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Random Name Selector - -This Python script helps you randomly select a specified number of names from a shortlist provided by the user. It takes a user input of shortlist names separated by commas and a second user input for the number of names to be randomly selected from the shortlist. It then prints the randomly selected names. - -## Features - -- Accepts a user-defined shortlist of names -- Validates the user input for the number of names to be selected -- Randomly selects and prints the specified number of names from the shortlist - -## Getting Started - -### Prerequisites - -- Python 3 and above installed on your system - -### Running the Script -Run the python script name-generator.py - -## Usage - -1. When prompted, enter the shortlist names separated by commas: - -Enter shortlist names separated by a comma: Beckham, Ronaldo, Messi, Silva, Aguero, Reus, DeBruyne - -2. When prompted, enter the number of names you want randomly selected from the shortlist: - -Enter the number of names you want randomly selected from the shortlist: 3 - -3. The script will then print the randomly selected names: - -Randomly selected names: Ronaldo, Aguero, DeBruyne -