首页 > 解决方案 > 我用逗号分隔的打印列表在星号处有一个逗号

问题描述

我正在编写一个小游戏,它将随机生成一个列表,并将其打印出来,不带括号,每个值都用逗号分隔。我唯一的问题是我在开头有一个文本字符串,Python 用逗号分隔文本字符串和列表。

这是我正在使用的代码:

if rollChoice == 6:
        for i in range(6):
            for i in range(4):
                newScore = random.randrange(1, 7)
                abilityScores.append(newScore)
            abilityScores.remove(min(abilityScores))
            for num in abilityScores:
                totalScore += num
            scoreList.append(totalScore)
            totalScore = 0
            abilityScores = []
        print ("Your ability scores are:", *scoreList, sep = ", ")

这是我得到的输出:

Your ability scores are:, 17, 6, 9, 13, 15, 14

标签: pythonlist

解决方案


sepin表示“print()使用此分隔符分隔每个参数”,并且您将标题文本和列表的所有成员作为不同的参数传递。尝试.join改用(尽管这需要将您的分数转换为字符串以进行打印):

print("Your ability scores are:", ", ".join([str(score) for score in scoreList]))

推荐阅读