0

I created this function definition in python:

def random_person():
    mylist = ["wounded priestress", "crying girl"]
    return random.choice(mylist)

Now I want to call that function in a print function in my code:

print("In the temple, you find a random_person().")

Unfortunately, it does not result in the strings I have chosen for my random function. This is what I get:

In the temple, you find a random_person().
7
  • 4
    Try print(f"In the temple, you find a {random_person()}.") Commented Apr 8, 2021 at 19:28
  • 2
    print("In the temple, you find a "+random_person()+".") is one way Commented Apr 8, 2021 at 19:29
  • 2
    Or print("In the temple, you find a", random_person(), ".") Commented Apr 8, 2021 at 19:29
  • 4
    if each word of a string was interpreted by python it would be very unconvenient right? Commented Apr 8, 2021 at 19:29
  • 1
    Or even print("In the temple, you find a {}.".format(random_person())) Commented Apr 8, 2021 at 19:32

2 Answers 2

2

Just thought I'd compile all the options I listed in the comments :)

print(f"In the temple, you find a {random_person()}.") # my personal favorite
print("In the temple, you find a", random_person(), ".")
print("In the temple, you find a {}.".format(random_person()))
print("In the temple, you find a %s." % random_person())

And @Jean-Francois' too:

print("In the temple, you find a "+random_person()+".")
Sign up to request clarification or add additional context in comments.

Comments

0

Give this a try.

import random
def random_person():
   mylist = ["wounded priestress", "crying girl"]
   return random.choice(mylist)
print(f"In the temple, you find a {random_person()}.")

It uses f-strings a way better way than +str()+

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.