Conversation
| while True: | ||
| num_names_str = input("Enter the number of names to be selected: ") | ||
| if num_names_str.isdigit(): | ||
| return int(num_names_str) | ||
| else: | ||
| print("Invalid input. Please Enter a postive interger.") |
There was a problem hiding this comment.
- Good short and concise function! Very easy to understand the whole function.
- Does the function stop getting inputs if 'else' condition is triggered?
- Does the 'if' condition consider negative values?
There was a problem hiding this comment.
Thank you for the comments!! In regards, to your question about the 'else' function, it will just tell you to "Enter the number of names to be selected:" again. The 'if' function does consider negative values when ran since the code only asks for positive integers and will pop up a message.
| def get_shortlist(): | ||
| # Reads the names that th user inputs and returns a list. | ||
| shortlist_str = input("Please enter names, separated by commas: ") | ||
| return [name.strip() for name in shortlist_str.split(',')] |
There was a problem hiding this comment.
Really like the concise code here - top work!
| return [name.strip() for name in shortlist_str.split(',')] | ||
|
|
||
| def get_num_names(): | ||
| # Reads the number of names to be selected from the list and returns the interger. |
There was a problem hiding this comment.
Could you add more comprehensive docstrings?
There was a problem hiding this comment.
i.e. function descriptions, parameters (in this case none!) and what is returned
|
|
||
| def select_names(shortlist, num_names): | ||
| # Selects num_names names randomly from the given list and returns a list. | ||
| return random.sample(shortlist, num_names) |
There was a problem hiding this comment.
Good use of the random library!
| num_names = get_num_names() | ||
|
|
||
| if num_names > len(shortlist): | ||
| print(f" Please enter the number of names to be selected: ({num_names}) is greater than the number of names in the shortlist ({len(shortlist)}.") |
There was a problem hiding this comment.
Could you put this into a loop? Currently, the user will have to restart the script - just something to think about going forward.
No description provided.