diff --git a/README.md b/README.md deleted file mode 100644 index 0e4526e..0000000 --- a/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# 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 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