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(" ")
insplit()
. - putting
name_list
afterrandom.sample()
(instead ofrandom.choices()
) rather than input_string
Thank you very much to the people who helped me!