Cilas Graff
Cilas Graff

Reputation: 13

Python random module formatting

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.

Upvotes: 1

Views: 44

Answers (1)

Mark
Mark

Reputation: 92440

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))

Upvotes: 1

Related Questions