Python 格式化随机模块函数的输出

Python 格式化随机模块函数的输出,python,random,format,output,Python,Random,Format,Output,我正在为自己编写一个程序,为我玩的这个游戏生成加载。我几乎完成了,我从使用random.choice切换到random.sample,以避免结果重复,但讨厌格式 print("He has", random.choice(kills) + ',', random.choice(kills) + ',', random.choice(kills) + ', and', random.choice(kills)) 产出: 他有双手扼流圈、叉子、叉子和干草叉刺 鉴于: print("He has",

我正在为自己编写一个程序,为我玩的这个游戏生成加载。我几乎完成了,我从使用random.choice切换到random.sample,以避免结果重复,但讨厌格式

print("He has", random.choice(kills) + ',', random.choice(kills) + ',', random.choice(kills) + ', and', random.choice(kills))
产出:

他有双手扼流圈、叉子、叉子和干草叉刺

鉴于:

print("He has", random.sample(kills, 4))
产出:

他有[‘膝盖折断’、‘下巴撕裂’、‘身体猛击’、‘窒息’]

如何获得输出类似random.choice()代码的示例?
谢谢

一种方法是迭代对象,将其添加到字符串中。请尝试以下操作:

choices = random.sample(kills,4) #put choices in variable
s = "He has " #begin output String
for(c in choices):
    s = s + c + "," #add choices to output string
s = s[:-1] #remove final comma
print(s) #print string

好的,等一下@ChristianDean@ChristianDean完成了。是的,我正要告诉你@Julian。干得好;-)+1您正在覆盖
random
(因此无法再次使用)并且正在将
str
应用于字符串(这是毫无意义的)。感谢您对导致错误的变量使用“random”,但是在我更改了变量名后,您的解决方案运行得非常好:ldOut=random.sample(kills,4)str_ldOut=“,”。join(str(x)for x in ldOut[:-1])print(“With”,str_ldOut,“and”,ldOut[-1])OP输出中的“and”呢?你最终将不得不做一些类似于@Julian在她的回答中所做的事情。
choices = random.sample(kills,4) #put choices in variable
s = "He has " #begin output String
for(c in choices):
    s = s + c + "," #add choices to output string
s = s[:-1] #remove final comma
print(s) #print string