因此,我试图用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[]
字典被忽略。另外,我打印它的方式是这样的,因为我希望它逐行打印,不带括号或引号
仅供参考-我使用的是Python3.8.2,我正在空闲状态下工作
因为我是新来的,不能发表评论,所以我只想猜一下您的代码的其余部分会是什么样子。 我开始添加这个来尝试复制您的输出
输出:
因此,在我的输出中,我认为您的代码按预期工作。你能解释一下你用来打印输出的是什么吗
编辑:显然还有一些格式化工作要做,也许你可以尝试使用^{} 或
\t
,或者最简单的方法就是在每个字典值中添加尾随空格字符,使其长度匹配,并相应地调整卡片edit2:因为您使用的是python3.8,所以可以使用formatted string literals(f-strings)。它们是一种非常快速且可读的动态格式化字符串的方法。 为此,可以在字符串中的python代码周围添加大括号,并在打开字符串之前预先添加
f
字符,如下所示:相关问题 更多 >
编程相关推荐