python - 我用逗号分隔的打印列表在星号处有一个逗号
问题描述
我正在编写一个小游戏,它将随机生成一个列表,并将其打印出来,不带括号,每个值都用逗号分隔。我唯一的问题是我在开头有一个文本字符串,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
解决方案
sep
in表示“print()
使用此分隔符分隔每个参数”,并且您将标题文本和列表的所有成员作为不同的参数传递。尝试.join
改用(尽管这需要将您的分数转换为字符串以进行打印):
print("Your ability scores are:", ", ".join([str(score) for score in scoreList]))
推荐阅读
- mybatis - MyBatis:如何仅针对一个特定语句将下划线结果映射到驼峰式?
- r - 当其他系列完全丢失时,使用 ggplot 调整闪避条形图的宽度
- assembly - 如何从另一个 MIPS 程序中保存来自一个 MIPS 程序的寄存器?
- stm32 - STM32CubeIDE 删除文件
- sql - 带有 listagg (distinct) 和 case 语句的 SQL 查询
- css - SVG 作为 React Material UI 中的伪背景图像
- react-native - 如何以原生方式完全禁用 react-native 中的 YellowBox ?不在 JavaScript 中
- bash - 处理多行并提取大量特定字符串的最佳方法是什么?
- grpc - GRPC:定义 proto post 端点同时接受正文和请求参数
- laravel - 无法检查 Laravel Dusk 中的复选框元素