0

I want user input to be joined to an item in an existing list

I tried formating the string in the list using %s

from random import randint
user_name = input("Name: ")

I want the %s to be the name the user input

my_list = ["Hello %s, nice to meet u",  
        "%s! what a wonderful name",
        "welcome %s"]
for m in my_list:
    print(randint(my_list)% user_name)

my output should be any of the items in the list accompanied by the user input i.e output: #Hello Mike, nice to meet u

where "Mike" is the user input

1
  • What do you think randint(my_list) does? You should try choice from the random module instead. Commented Jan 14, 2019 at 22:43

3 Answers 3

3

I am not sure what the logic your code is exhibiting is intended to do, however, here is my interpretation of what you want done.

from random import randint

user_name = input("Name: ")

my_list = ["Hello %s, nice to meet u",
        "%s! what a wonderful name",
        "welcome %s"]

print(choice(my_list) % user_name)

This will print one of the items from the list (at random) with the input appended in the desired location.

Examples:

Name: Tim
Hello Tim, nice to meet u

Name: Pam
Pam! what a wonderful name

Name: Jen
welcome Jen

Edit

Utilize choice instead of randint for clarity/ease/etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @TimKlein
2

I'm more used to using long format strings.

from random import choice
user_name = input("Name: ")
my_list = ["Hello {name}, nice to meet u",  
        "{name}! what a wonderful name",
        "welcome {name}"]
for m in my_list:
    print(choice(my_list).format(name=user_name))

But changing your randint to choice should also work in your case.

  • randint return a random number between min and max
  • choice randomly choose an element in a list

1 Comment

@fill_J Make sure to select this as the correct answer if it helped you.
0

I think other answers are better but I did the same thing by converting the list to string and then again to list.

from random import randint
user_name = input("Name: ")

my_list = ["Hello %s, nice to meet u",  "%s! what a wonderful name", "welcome %s"]

# convert/flatten the list to string
list_to_string =  "::".join(str(x) for x in my_list)

# Replace %s with the username
replaced_string = list_to_string.replace("%s",user_name )

# convert string to list 
string_to_list = replaced_string.split("::")

print(string_to_list)

2 Comments

What is the OP add a string with :: inside? That sounds like a dangerous method. At least, use list comphrensions to do something like [s.replace("%s", user_name) for s in my_list]
:: is just a place holder so that I can convert the string back to list. But, your answer is much better with the computation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.