python 标志之间的稳定距离
python steady distance between signs
在我的代码中,当数字在末尾增加一个数字时,组成模拟框的字符会移动。当它从 9 到 10,从 99 到 100 时,它希望垂直条不要移动。目前,在 {x} 和 | 之间空格被插入。我的代码是:
import time
import os
x = 0
y = 0
while True:
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
| {x} |
| {y} |
| |
| -------------------------------------------------------|
""")
time.sleep(0.5)
x += 9
y += 9
os.system('cls' if os.name == 'nt' else 'clear')
您可以计算出数字后的填充长度。我发现在你的例子中应该有 55 个字符 space 包括你显示的数字。
我创建了一个辅助函数,它结合使用 str
和 len
来计算数字的长度,并通过减少数字占用的 space 的数量来计算填充。
import time
import os
def get_padding(length_of_window, i):
return (length_of_window - len(str(i))) * ' '
def main():
x = 0
y = 0
length_of_window = 55
while True:
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
| {x}{get_padding(length_of_window, x)}|
| {y}{get_padding(length_of_window, y)}|
| |
| -------------------------------------------------------|
""")
time.sleep(0.5)
x += 9
y += 9
os.system('cls' if os.name == 'nt' else 'clear')
if __name__ == '__main__':
main()
您也可以直接使用 String Format Syntax,无需串联或辅助函数。
import time
import os
def main():
x = 0
y = 0
length_of_window = 55
while True:
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
| {x:<{length_of_window}}|
| {y:<{length_of_window}}|
| |
| -------------------------------------------------------|
""")
time.sleep(0.5)
x += 9
y += 9
os.system('cls' if os.name == 'nt' else 'clear')
if __name__ == '__main__':
main()
要打印带有固定空格的数字,您可以使用方法 .format
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
"""
+
"| {:<55d}|\n | {:<55d}|".format(x,y)
+
"""
| |
| -------------------------------------------------------|
""")
有关更多格式选项,请参阅 here
在我的代码中,当数字在末尾增加一个数字时,组成模拟框的字符会移动。当它从 9 到 10,从 99 到 100 时,它希望垂直条不要移动。目前,在 {x} 和 | 之间空格被插入。我的代码是:
import time
import os
x = 0
y = 0
while True:
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
| {x} |
| {y} |
| |
| -------------------------------------------------------|
""")
time.sleep(0.5)
x += 9
y += 9
os.system('cls' if os.name == 'nt' else 'clear')
您可以计算出数字后的填充长度。我发现在你的例子中应该有 55 个字符 space 包括你显示的数字。
我创建了一个辅助函数,它结合使用 str
和 len
来计算数字的长度,并通过减少数字占用的 space 的数量来计算填充。
import time
import os
def get_padding(length_of_window, i):
return (length_of_window - len(str(i))) * ' '
def main():
x = 0
y = 0
length_of_window = 55
while True:
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
| {x}{get_padding(length_of_window, x)}|
| {y}{get_padding(length_of_window, y)}|
| |
| -------------------------------------------------------|
""")
time.sleep(0.5)
x += 9
y += 9
os.system('cls' if os.name == 'nt' else 'clear')
if __name__ == '__main__':
main()
您也可以直接使用 String Format Syntax,无需串联或辅助函数。
import time
import os
def main():
x = 0
y = 0
length_of_window = 55
while True:
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
| {x:<{length_of_window}}|
| {y:<{length_of_window}}|
| |
| -------------------------------------------------------|
""")
time.sleep(0.5)
x += 9
y += 9
os.system('cls' if os.name == 'nt' else 'clear')
if __name__ == '__main__':
main()
要打印带有固定空格的数字,您可以使用方法 .format
print(f"""
| -------------------------------------------------------|
| great quote about life |
| that can change every 0.5 sec |
"""
+
"| {:<55d}|\n | {:<55d}|".format(x,y)
+
"""
| |
| -------------------------------------------------------|
""")
有关更多格式选项,请参阅 here