I've just finished typing up some code for a game I'm working on, however I need to use the "format" function to make the output align properly, I tried using it on this bit of code for example:
我刚刚为我正在处理的游戏输入了一些代码,但是我需要使用“format”函数来使输出正确对齐,我尝试在这段代码上使用它,例如:
print(" ",humanscore," - ",compscore)
in order to make sure the values of the variables don't shift their position, but it comes up with an error saying "TypeError: format() takes at most 2 arguments (5 given)", So basically I'm just wondering how you use the format function to align lines of code like this. (the spaces inbetween were a cheap way of aligning stuff without the format function. )
为了确保变量的值不会改变它们的位置,但它会出现一个错误,说“TypeError:format()最多需要2个参数(给定5个)”,所以基本上我只是想知道如何您使用format函数来对齐这样的代码行。 (中间的空格是一种没有格式功能的便宜的对齐方式。)
2 个解决方案
#1
print(" {}, - {}".format(humanscore, compscore))
Assuming your scores are variable and you have this in a for loop.
假设你的分数是可变的,你可以在for循环中使用它。
#2
The format
method is called on a string and substitutes formatted quantities into the string at places you indicate with markers using braces ({
,}
). There's a whole mini-language to Python's new(er) string formatting, but for your purposes, to make the scores align you should decide how many characters the maximum score will take up and use '{:<n>d}'
where <n>
is that maximum. For example if you won't have (integer) scores higher than 999999
,
在字符串上调用format方法,并使用大括号({,})将格式化的数量替换为使用标记指示的位置的字符串。 Python的新(呃)字符串格式有一个完整的迷你语言,但为了您的目的,要使得分数对齐,您应该决定最高分数将占用多少个字符并使用'{:
In [8]: humanscore1 = 12
In [9]: compscore1 = 933
In [10]: humanscore2 = 8872
In [11]: compscore2 = 12212
print('{:6d} - {:6d}'.format(humanscore1, compscore1))
print('{:6d} - {:6d}'.format(humanscore2, compscore2))
12 - 933
8872 - 12212
To center the numbers instead of aligning them to the right of their fields, use the ^
specifier:
要使数字居中而不是将它们对齐到其字段的右侧,请使用^说明符:
print('{:^6d} - {:^6d}'.format(humanscore2, compscore2))
print('{:^6d} - {:^6d}'.format(humanscore1, compscore1))
8872 - 12212
12 - 933
#1
print(" {}, - {}".format(humanscore, compscore))
Assuming your scores are variable and you have this in a for loop.
假设你的分数是可变的,你可以在for循环中使用它。
#2
The format
method is called on a string and substitutes formatted quantities into the string at places you indicate with markers using braces ({
,}
). There's a whole mini-language to Python's new(er) string formatting, but for your purposes, to make the scores align you should decide how many characters the maximum score will take up and use '{:<n>d}'
where <n>
is that maximum. For example if you won't have (integer) scores higher than 999999
,
在字符串上调用format方法,并使用大括号({,})将格式化的数量替换为使用标记指示的位置的字符串。 Python的新(呃)字符串格式有一个完整的迷你语言,但为了您的目的,要使得分数对齐,您应该决定最高分数将占用多少个字符并使用'{:
In [8]: humanscore1 = 12
In [9]: compscore1 = 933
In [10]: humanscore2 = 8872
In [11]: compscore2 = 12212
print('{:6d} - {:6d}'.format(humanscore1, compscore1))
print('{:6d} - {:6d}'.format(humanscore2, compscore2))
12 - 933
8872 - 12212
To center the numbers instead of aligning them to the right of their fields, use the ^
specifier:
要使数字居中而不是将它们对齐到其字段的右侧,请使用^说明符:
print('{:^6d} - {:^6d}'.format(humanscore2, compscore2))
print('{:^6d} - {:^6d}'.format(humanscore1, compscore1))
8872 - 12212
12 - 933