How do I make my input pick up words rather than letters? (Python)

Question:

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!

Asked By: Jakob

||

Answers:

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.

Answered By: b9s

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)

Answered By: Vlad Sirbu
  • 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

Answered By: Deep Dalsania
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.