我有這個問題。我正在嘗試在 python 中擲骰子。但是,每當我打印它的值時,它都會附加 [""]。有沒有辦法刪除這個?例子。import randomchoices = ["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))輸出。[“正面”]、[“反面”] 或 [“硬幣落在它的一邊。這是平局!”]額外的括號和引號真的很煩人,因為我正試圖將其發布到文本頻道。
1 回答

ibeautiful
TA貢獻1993條經驗 獲得超6個贊
choices()即使您只要求一個值,也會返回一個列表。你可以通過索引來獲取這個值:
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
您還可以解壓縮一個值以獲得相同的效果:
print("{}".format(*rancoin))
添加回答
舉報
0/150
提交
取消