1

I have this problem. I'm trying to make a dice roll in python. But whenever i print its value it has [""] attached. Is there a way to remove this?

Example.

import random

choices = ["Heads", "The Coin landed on it's side. It's a draw!", "Tails"]
rancoin = random.choices(choices, weights = [10, 1, 10], k = 1)

print("{}".format(rancoin))

Outputs.

["Heads"], ["Tails"] or ["The Coin landed on it's side. It's a draw!"]

It's really annoying having the extra brackets and quotation marks, since i'm trying to make it post to a text channel.

1 Answer 1

1

choices() returns a list even if you are only asking for one value. You can just grab this value by indexing it:

import random 

choices = ["Heads", "The Coin landed on it's side. It's a draw!", "Tails"] 
rancoin = random.choices(choices, weights = [10, 1, 10], k = 1)

print("{}".format(rancoin[0])) # note the [0] to get the first (and only) item
# Heads

You could also unpack the one value for the same effect:

print("{}".format(*rancoin))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I can't believe i didn't think of that.

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.