【问题标题】:Concatenating a dictionary item with a string inside a list将字典项与列表中的字符串连接起来
【发布时间】:2020-05-25 22:38:37
【问题描述】:

所以我正在尝试在 python 中创建游戏 Yahtzee,并且我正在尝试在我创建的表格中放置点。虽然,因为字符串是不可变的,所以我尝试从列表(包含表)中检索字典项并将其与字符串的其余部分连接起来。但由于某种原因,我的输出只是排除了字典项,就好像它不存在一样。

代码如下:

points = {
    "1" : "Ones",
    "2" : "Twos",
    "3" : "Threes",
    "4" : "Fours",
    "5" : "Fives",
    "6" : "Sixes"
}


avaliable_grid = [
    " _____________________________          _____________________________",
    "|           |Player 1|Player 2|        |           |Player 1|Player 2|",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Ones       |" + points["1"] + "|        |        |3 of a Kind|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Twos       |" + points["2"] + "|        |        |4 of a Kind|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Threes     |" + points["3"] + " |        |        |Full House |        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Fours      |" + points["4"] + "|        |        |S. Straight|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Fives      |" + points["5"] + "|        |        |L. Straight|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Sixes      |" + points["6"] + "|        |        |Chance     |        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|#############################|        |YAHTZEE    |        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Sum        |        |        |        |#############################|",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Bonus      |        |        |        |TOTAL SCORE|        |        |",
    " -----------------------------          ----------------------------- ",
    ]


def print_table(table):
    for i in table:
        print(i)

print_table(avaliable_grid)    

输出:

 _____________________________          _____________________________
|           |Player 1|Player 2|        |           |Player 1|Player 2|
|-----------|--------|--------|        |-----------|--------|--------|
|Ones       |        |        |        |3 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Twos       |        |        |        |4 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Threes     |        |        |        |Full House |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fours      |        |        |        |S. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fives      |        |        |        |L. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sixes      |        |        |        |Chance     |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|#############################|        |YAHTZEE    |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sum        |        |        |        |#############################|
|-----------|--------|--------|        |-----------|--------|--------|
|Bonus      |        |        |        |TOTAL SCORE|        |        |
 -----------------------------          ----------------------------- 

由于某种原因,points[] 字典被忽略。另外,我打印它的方式就是这样,因为我希望它逐行打印,不带括号或引号。

供参考-我正在使用 python 3.8.2,并且正在通过 IDLE 工作。

【问题讨论】:

  • 我运行它时效果很好。
  • 尝试在其他地方运行它,我认为您使用 IDLE 错误。
  • 我还能在哪里运行它?对python游戏相当陌生,我不知道在哪里可以。

标签: python list dictionary concatenation


【解决方案1】:

由于我是新手,无法发表评论,我只是猜测您的其余代码会是什么样子。 我首先添加它以尝试重现您的输出。

for row in avaliable_grid:
    print(row)

输出:

_____________________________          _____________________________
|           |Player 1|Player 2|        |           |Player 1|Player 2|
|-----------|--------|--------|        |-----------|--------|--------|
|Ones       |Ones|        |        |3 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Twos       |Twos|        |        |4 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Threes     |Threes |        |        |Full House |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fours      |Fours|        |        |S. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fives      |Fives|        |        |L. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sixes      |Sixes|        |        |Chance     |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|#############################|        |YAHTZEE    |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sum        |        |        |        |#############################|
|-----------|--------|--------|        |-----------|--------|--------|
|Bonus      |        |        |        |TOTAL SCORE|        |        |
 -----------------------------          ----------------------------- 

因此,在我的输出中,我认为您的代码按预期工作。你能解释一下你用来实际打印输出的内容吗?

编辑:显然还有一些格式需要做,也许你可以尝试使用pprint\t,或者最简单的方法是在每个字典值中添加尾随空格字符以获得它们的长度匹配并相应调整卡片。

points = {
    "1" : "Ones  ",
    "2" : "Twos  ",
    "3" : "Threes",
    "4" : "Fours ",
    "5" : "Fives ",
    "6" : "Sixes "
}

edit2:由于您使用的是 python 3.8,因此您可以使用formatted string literals(f-strings)。它们是动态格式化字符串的一种非常快速且易读的方式。 为此,您可以在字符串中的 python 代码周围添加花括号,并在打开字符串之前添加 f 字符,如下所示:

f"|Ones       | {points["1"]} |        |        |3 of a Kind|        |" 

【讨论】:

  • 我明白了!很高兴知道我们正在以相同的方式打印表格,您是否设法按照您想要的方式打印字典值?
  • 当我使用新添加的打印功能运行代码时,它对我来说仍然可以正常工作——在 IDLE 中——只是格式有点不对。字典值仍然没有显示吗?
  • 嘿,别担心!将来我可以建议一些rubber duck debugging :p。关于 fstrings,请广而告之!我听说它们比以前的方法更快,但我还没有遇到很多使用它们的人。
  • 如果你确定你的字典[key] 指向正确的值,`f'{dict[key]}' 应该显示那个值。更改词典时,您必须重新打印整张卡片。
  • 啊,很高兴它成功了。如果这个或任何答案已经解决了您的问题,请通过单击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。 (这是从here 偷来的,因为我认为这样问会很不礼貌,但由于我们都是新手,我想我会试一试。
猜你喜欢
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多