【发布时间】: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