0

I'm trying to make a random name picker for me and my friends.

The problem I have is that if I enter a bunch of names the output gives me 5 separate letters picked from our names rather than our full names.

Example of the output I receive

Here's my code so far.

import random

print("Random Name Picker")

input_string = str(input("Input names: "))
if input_string == "exit":
    exit()
nameList = input_string.split()
print("Our contestants for this round are:", nameList)

sampled_list = random.choices(input_string, k=5)
print("Our winners are:", sampled_list)

Any help would be much appreciated. Let me know if you need any more information.

Thank you!

EDIT: I've fixed it, and I also no longer get repeat names in my code. Here is the fixed program for anyone in the future

import random

print("Random Name Picker")

input_string = input("Input names: ")
if input_string == "exit":
    exit()
nameList = input_string.split() # by default it will split by space 
print("Our contestants for this round are:", nameList)

sampled_list = random.sample(nameList, k=5)
print("Our winners are:", sampled_list)

The main changes are:

  • including () after input_string.split() to appropriately split my input. Also by default, It will split by space so no need to indicate (" ") in split().
  • putting name_list after random.sample() (instead of random.choices()) rather than input_string

Thank you very much to the people who helped me!

dan1st
  • 12,568
  • 8
  • 34
  • 67
Jakob
  • 5
  • 4

3 Answers3

0

You need to pick random choices from the nameList instead of the input_string.

The input_string is a string that hasn't been split yet, so random.choices returns characters. The nameList is a list of strings, so random.choices will return entries of the list.

b9s
  • 517
  • 4
  • 13
  • 1
    Awesome! It's working as intended now. Thank you for explaining it, the helps me wrap my head around it. Cheers :) – Jakob May 12 '20 at 10:52
0

You need to split your input string by ' ' and get random choices from the resulting list. Something like:


print("Random Name Picker")

input_string = str(input("Input names: "))
if input_string == "exit":
    exit()
nameList = input_string.split(' ')
print("Our contestants for this round are:", nameList)

sampled_list = random.choices(nameList, k=5)
print("Our winners are:", sampled_list)

Of course the result might have the same name more than once so just make it a set(sampled_list)

Vlad Sirbu
  • 108
  • 10
  • Ah! Thank you for pointing that out for me, I just encountered that problem. I've included set(sampled_list) on my last sampled_list and I'm no longer getting repeats of names. Only problem now is that doesn't always return 5 names, i've received from 2 to 5 so far. Anyway idea as to why? – Jakob May 12 '20 at 11:09
  • It's because a name is picked randomly more than once and are reduced during set operation. You could remove a name from list once it has been picked. – Vlad Sirbu May 12 '20 at 11:32
0
  • As I understand your question you need a random list of winners from the entered name so there is a small mistake in your program. Here is your program.

    import random

    print("Random Name Picker")

    input_string = input("Input names: ")
    if input_string == "exit":
        exit()
    print("Our contestants for this round are:", input_string.split(","))
    print("Our winners are:", random.choices(input_string.split(","), k=5))

  • Firstly, You are not splitting an input_string when you pass to the random.choices() so it will return a list of characters as @b9s said.
  • Also, you are using str(input("Input names: ")). If the prompt argument is present, it is written to standard output without a trailing newline. The input() then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
  • If you want unique random names then you can use the below code.
    import random

    print("Random Name Picker")

    input_string = input("Input names: ")
    if input_string == "exit":
        exit()
    name_list = input_string.split(",")
    print("Our contestants for this round are:", name_list)
    s = int(input("Enter a Positive Sample Size : "))
    print("Our winners are:", random.sample(name_list,k=s) if s <= len(name_list) else "Sample larger than population or is negative")

  • Note that if you are not passing , separated values then only use split(). It by default split string using space.

Reference: Choices Function and Input Function

Deep Dalsania
  • 375
  • 3
  • 8
  • 22
  • Oh hey! This works perfectly apart from some names coming up twice (as Vlad Sirbu mentioned above). Do you know any way to fix that? – Jakob May 12 '20 at 11:15
  • Yes, @Jakob. If you want to remove this redundant names then You can use [Sample](https://docs.python.org/3/library/random.html) function for this but if your sample size is larger then population size then it will raise a `ValueError: Sample larger than population or is negative` So you have to check first in if the condition. – Deep Dalsania May 12 '20 at 13:00
  • Also, You can check this [StackOverflow Answer](https://stackoverflow.com/questions/30784119/python-random-name-generator-without-getting-dupes) – Deep Dalsania May 12 '20 at 13:12