打印变量改变字符的位置

时间:2016-11-24 12:26:01

标签: python symbols

我正在编写一个游戏项目,我正在通过代码来完成代码中的一些小错误和功能。我在尝试在由文本符号组成的图形框中打印用户统计信息时遇到问题。 以下是代码

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是什么,我如何将角色保留在同一个地方。 代码中没有错误,只是一个查询。

4 个答案:

答案 0 :(得分:3)

您可以使用PyFormat来执行填充和对齐选项。

list<T>

输出:

let rec cond = function
  | NewUnionCase (c, [NewTuple [condition; value]; tail]) 
      when c.Name = "Cons" && c.DeclaringType.IsGenericType &&
        c.DeclaringType.GetGenericTypeDefinition() = typedefof<_ list> -> 
      Some(condition, value, tail)
  | _ -> 
      None

虽然变量len更改整体对齐方式仍然相同。

答案 1 :(得分:1)

由于每个统计行的第一部分(12个字符)都有固定的长度,并且标题需要53个字符,如果您的值只有一个数字,则需要在结束字符' '*(40 - len(str(p1_<whatever>)))之前放置39个空格,38如果两位数,......等等。我,e:# -*- 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 ⚜ ⚜===================================================⚜ 库来证明不同场景的正确功能:

'use strict';

var linesElement = 3; //it will truncate at 3 lines.
var truncateElement = document.getElementById('truncateme');
var truncateText = truncateElement.textContent;

var getLineHeight = function getLineHeight(element) {
  var lineHeight = window.getComputedStyle(truncateElement)['line-height'];
  if (lineHeight === 'normal') {
    // sucky chrome
    return 1.16 * parseFloat(window.getComputedStyle(truncateElement)['font-size']);
  } else {
    return parseFloat(lineHeight);
  }
};

linesElement.addEventListener('change', function () {
  truncateElement.innerHTML = truncateText;
  var truncateTextParts = truncateText.split(' ');
  var lineHeight = getLineHeight(truncateElement);
  var lines = parseInt(linesElement.value);

  while (lines * lineHeight < truncateElement.clientHeight) {
    console.log(truncateTextParts.length, lines * lineHeight, truncateElement.clientHeight);
    truncateTextParts.pop();
    truncateElement.innerHTML = truncateTextParts.join(' ') + '...';
  }
});

答案 2 :(得分:1)

为什么要这么复杂?只需使用具有指定宽度的标准字符串格式替换:

rp.skipped.issue = option to mark skipped tests as not 'To Investigate' items on Server side. Boolean values: TRUE - skipped tests considered as issues and will be mark as 'To Investigate'. FALSE - skipped tests will not be mark as 'To Investigate' on portal.

或者,如果您自己对齐标签:

print ("⚜================== Player 1 City ==================⚜")
print ("⚜ %-7s = %-39d ⚜" % ("BaseHP", p1_basehp))
print ("⚜ %-7s = %-39d ⚜" % ("Troops", p1_troops))
etc.

答案 3 :(得分:0)

所以你需要做的是增加许多空间,因为=的差异是该str值的底线和长度。

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 ("⚜===================================================⚜")