-
Notifications
You must be signed in to change notification settings - Fork 31
completed project #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of functions: The code defines a function random_name_selector to perform the core task of randomly selecting names. This is good programming practice as it makes the code more modular and reusable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code uses consistent style throughout, including indentation, spacing, and variable naming. This makes the code easy to read and understand. |
||
| # 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." | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use f-strings for error message: The code currently returns an error message as a string. It would be more Pythonic to use f-strings to format the error message, like so: return f"Invalid input: number of selections should be between 1 and {len(names)}.". |
||
|
|
||
| # 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() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of comments: The code contains useful comments that explain what each part of the code does. This makes it easier for others to understand and maintain the code.