打印的变量改变字符的位置
Printed variable changes position of character
我正在编写一个游戏项目,正在检查代码以完成代码中的一些小错误和功能。我在尝试在由文本符号组成的图形框中打印用户统计信息时遇到问题。
这是代码
print ("⚜================== Player 1 City ==================⚜")
print ("⚜BaseHP = " + str (p1_basehp))
print ("⚜Troops = " + str (p1_troops))
print ("⚜Archers = " + str (p1_archers))
print ("⚜Food = " + str (p1_food))
print ("⚜===================================================⚜")
当代码在游戏中 运行 时,它看起来像这样。
⚜================== Player 1 City ==================⚜
⚜BaseHP = 1000
⚜Troops = 20
⚜Archers = 30
⚜Food = 500
⚜===================================================⚜
但我真的很想将用户统计信息放在一个框中,就像这样。
⚜================== Player 1 City ==================⚜
⚜BaseHP = 1000 ⚜
⚜Troops = 20 ⚜
⚜Archers = 30 ⚜
⚜Food = 500 ⚜
⚜===================================================⚜
我知道这很挑剔,但我想完成游戏的全部工作,而不是让游戏中的部分看起来未完成。问题是 4 个列表根据用户的操作在整个游戏中改变值,因此无法预测它们,这意味着当打印出值时,框右侧的符号不在一条直线上。无论 4 个列表 p1_basehp、p1_troops、p1_archers 和 p1_food 是什么,我如何将字符保持在同一个位置。
代码没有错误,只是查询而已。
因此,您需要做的是增加那么多 space,因为 =
的区别在于该字符串值的底线和长度。
print ("⚜BaseHP = " + str (p1_basehp),' '*(40 - len(str(p1_basehp))),'*')
print ("⚜Troops = " + str (p1_troops),' '*(40 - len(str(p1_troops))),'*')
print ("⚜Archers = " + str (p1_archers),' '*(40 - len(str(p1_archers))),'*')
print ("⚜Food = " + str (p1_food),' '*(40 - len(str(p1_food))),'*')
print ("⚜===================================================⚜")
由于每个统计行的第一部分的长度是固定的(12 个字符),而您的 header 占用 53 个字符,因此您需要在结束字符 ⚜
之前放置 39 个空格,如果您的值只有一位数,如果是两位数则为 38,...等等。我,e:' '*(40 - len(str(p1_<whatever>)))
# -*- coding: utf-8 -*-
from random import randint
p1_archers = randint(1, 1000)
p1_basehp = randint(1, 1000)
p1_food = randint(1, 1000)
p1_troops = randint(1, 1000)
print ("⚜================== Player 1 City ==================⚜")
print ("⚜BaseHP = " + str (p1_basehp)) + ' '*(40 - len(str(p1_basehp))) + '⚜'
print ("⚜Troops = " + str (p1_troops)) + ' '*(40 - len(str(p1_troops))) + '⚜'
print ("⚜Archers = " + str (p1_archers)) + ' '*(40 - len(str(p1_archers))) + '⚜'
print ("⚜Food = " + str (p1_food)) + ' '*(40 - len(str(p1_food))) + '⚜'
print ("⚜===================================================⚜")
注意:我使用 random
库来证明不同场景下的正确功能:
⚜===================================================⚜
[Vostro-3350 workspace]$ python so.py
⚜================== Player 1 City ==================⚜
⚜BaseHP = 846 ⚜
⚜Troops = 334 ⚜
⚜Archers = 685 ⚜
⚜Food = 94 ⚜
⚜===================================================⚜
[Vostro-3350 workspace]$ python so.py
⚜================== Player 1 City ==================⚜
⚜BaseHP = 70 ⚜
⚜Troops = 8 ⚜
⚜Archers = 306 ⚜
⚜Food = 411 ⚜
⚜===================================================⚜
您可以使用 PyFormat 通过填充和对齐选项来执行此操作。
In [120]: p1 = 1
In [121]: p2 = 123
In [122]: p3 = 12345
In [123]: print "** BaseHP = " + '{:10}'.format(str(p1)) + '*'
In [124]: print "** BaseHP = " + '{:10}'.format(str(p2)) + '*'
In [125]: print "** BaseHP = " + '{:10}'.format(str(p3)) + '*'
输出:
** BaseHP = 1 *
** BaseHP = 123 *
** BaseHP = 12345 *
虽然变量的 len 改变了,但整体对齐方式保持不变。
为什么搞得这么复杂?只需使用具有指定宽度的标准字符串格式替换:
print ("⚜================== Player 1 City ==================⚜")
print ("⚜ %-7s = %-39d ⚜" % ("BaseHP", p1_basehp))
print ("⚜ %-7s = %-39d ⚜" % ("Troops", p1_troops))
etc.
或者如果您自己对齐标签:
print("⚜BaseHP = %-39d ⚜" % p1_basehp)
etc.
我正在编写一个游戏项目,正在检查代码以完成代码中的一些小错误和功能。我在尝试在由文本符号组成的图形框中打印用户统计信息时遇到问题。 这是代码
print ("⚜================== Player 1 City ==================⚜")
print ("⚜BaseHP = " + str (p1_basehp))
print ("⚜Troops = " + str (p1_troops))
print ("⚜Archers = " + str (p1_archers))
print ("⚜Food = " + str (p1_food))
print ("⚜===================================================⚜")
当代码在游戏中 运行 时,它看起来像这样。
⚜================== Player 1 City ==================⚜
⚜BaseHP = 1000
⚜Troops = 20
⚜Archers = 30
⚜Food = 500
⚜===================================================⚜
但我真的很想将用户统计信息放在一个框中,就像这样。
⚜================== Player 1 City ==================⚜
⚜BaseHP = 1000 ⚜
⚜Troops = 20 ⚜
⚜Archers = 30 ⚜
⚜Food = 500 ⚜
⚜===================================================⚜
我知道这很挑剔,但我想完成游戏的全部工作,而不是让游戏中的部分看起来未完成。问题是 4 个列表根据用户的操作在整个游戏中改变值,因此无法预测它们,这意味着当打印出值时,框右侧的符号不在一条直线上。无论 4 个列表 p1_basehp、p1_troops、p1_archers 和 p1_food 是什么,我如何将字符保持在同一个位置。 代码没有错误,只是查询而已。
因此,您需要做的是增加那么多 space,因为 =
的区别在于该字符串值的底线和长度。
print ("⚜BaseHP = " + str (p1_basehp),' '*(40 - len(str(p1_basehp))),'*')
print ("⚜Troops = " + str (p1_troops),' '*(40 - len(str(p1_troops))),'*')
print ("⚜Archers = " + str (p1_archers),' '*(40 - len(str(p1_archers))),'*')
print ("⚜Food = " + str (p1_food),' '*(40 - len(str(p1_food))),'*')
print ("⚜===================================================⚜")
由于每个统计行的第一部分的长度是固定的(12 个字符),而您的 header 占用 53 个字符,因此您需要在结束字符 ⚜
之前放置 39 个空格,如果您的值只有一位数,如果是两位数则为 38,...等等。我,e:' '*(40 - len(str(p1_<whatever>)))
# -*- coding: utf-8 -*-
from random import randint
p1_archers = randint(1, 1000)
p1_basehp = randint(1, 1000)
p1_food = randint(1, 1000)
p1_troops = randint(1, 1000)
print ("⚜================== Player 1 City ==================⚜")
print ("⚜BaseHP = " + str (p1_basehp)) + ' '*(40 - len(str(p1_basehp))) + '⚜'
print ("⚜Troops = " + str (p1_troops)) + ' '*(40 - len(str(p1_troops))) + '⚜'
print ("⚜Archers = " + str (p1_archers)) + ' '*(40 - len(str(p1_archers))) + '⚜'
print ("⚜Food = " + str (p1_food)) + ' '*(40 - len(str(p1_food))) + '⚜'
print ("⚜===================================================⚜")
注意:我使用 random
库来证明不同场景下的正确功能:
⚜===================================================⚜
[Vostro-3350 workspace]$ python so.py
⚜================== Player 1 City ==================⚜
⚜BaseHP = 846 ⚜
⚜Troops = 334 ⚜
⚜Archers = 685 ⚜
⚜Food = 94 ⚜
⚜===================================================⚜
[Vostro-3350 workspace]$ python so.py
⚜================== Player 1 City ==================⚜
⚜BaseHP = 70 ⚜
⚜Troops = 8 ⚜
⚜Archers = 306 ⚜
⚜Food = 411 ⚜
⚜===================================================⚜
您可以使用 PyFormat 通过填充和对齐选项来执行此操作。
In [120]: p1 = 1
In [121]: p2 = 123
In [122]: p3 = 12345
In [123]: print "** BaseHP = " + '{:10}'.format(str(p1)) + '*'
In [124]: print "** BaseHP = " + '{:10}'.format(str(p2)) + '*'
In [125]: print "** BaseHP = " + '{:10}'.format(str(p3)) + '*'
输出:
** BaseHP = 1 *
** BaseHP = 123 *
** BaseHP = 12345 *
虽然变量的 len 改变了,但整体对齐方式保持不变。
为什么搞得这么复杂?只需使用具有指定宽度的标准字符串格式替换:
print ("⚜================== Player 1 City ==================⚜")
print ("⚜ %-7s = %-39d ⚜" % ("BaseHP", p1_basehp))
print ("⚜ %-7s = %-39d ⚜" % ("Troops", p1_troops))
etc.
或者如果您自己对齐标签:
print("⚜BaseHP = %-39d ⚜" % p1_basehp)
etc.