如何使用\ t在Python中缩进代码

时间:2018-10-23 01:18:46

标签: python indentation

我是python的新手,在掷骰子游戏时遇到麻烦。我希望攻击者和防御者健康状况的输出位于屏幕的另一侧,而不是其他输出下方。无论如何,是否有缩进输出,因为当我尝试输出时,我得到的只是str和int错误。

我的代码:

当前输出:

2 个答案:

答案 0 :(得分:2)

您可以尝试在字符串中的文本之后添加\n,而不是多次添加打印内容 前print("The Quick Brown fox jumps over the lazy dog \n\n") 您可以尝试的选项卡 print("attacker health: \t\t Defender Health\n",attacker_health,"\t\t\t\t",defender_health) 您可以更改编号。根据您的屏幕大小在\t中。 您应该尝试\n\t而不是多次键入print。 希望这可以帮助你。

答案 1 :(得分:0)

使用Python3的内置字符串格式,您可以并排打印它们(这是我从您的问题中所了解的方式)。

print('{name1}:{health1} \t\t\t\t\t {name2}:{health2}'.format(name1=str(dice1 + dice2 + dice3),health1=attacker_health,name2=str(dice4 = dice5),health2=defender_health))

在上述解决方案中,{}中的所有内容都是变量的占位符,它们作为关键字参数提供给format方法。希望这会有所帮助。