Python3使用.format()打印的语句以相反的方式打印



我正在尝试用Python 3创建一个简单的基于终端的游戏。我使用cmd模块来制作菜单,并在其中使用测试脚本。这是代码。

from assets import *
from cmd import Cmd
from test import TestFunction
import base64
class Grimdawn(Cmd):
#irrelevant code removed
def do_test(self, args):
"""Run a test script. Requires dev password."""
password = str(base64.b64decode("""REDACTED"""))
if len(args) == 0:
print("Please enter the password for accessing the test script.")
elif args == password:
test_args = input('Enter test command.n')
try:
TestFunction(test_args.upper())
except IndexError:
print('Enter a command.')

else:
print("Incorrect password.")

测试功能如下所示。

edit:基本字符不同,因为我刚刚测试一种新的打印格式,还没有编辑其他if语句

from assets import *
def TestFunction(args):
player1 = BaseCharacter()
player2 = BerserkerCharacter('Jon', 'Snow')
player3 = WarriorCharacter('John', 'Smith')
player4 = ArcherCharacter('Alexandra', 'Bobampkins')
#//removed irrelevant code
if args == "BASE_OFFENSE":
return('Base Character: Offensiven-------------------------n{}'.format(player1.show_player_stats("offensive")))
#. . .
elif args == "ARCHER_OFFENSE":
print('Archer Character: Offensiven-------------------------n{}'.format(player4.show_player_stats("offensive")))
return
#. . .

它应该打印Archer Character: Offensive,后面跟着一行,后面跟着格式化的代码。但当我打印它时,这是终端输出。

Joshua Brenneman - Grimdawn v0.0.2 |
> test *PASSWORD REDACTED*
Enter test command.
ARCHER_OFFENSE
Strength: 14.25
Agility: 10
Critical Chance: 50.0
Spell Power: 15
Intellect: 5
Speed: 6.25
Archer Character: Offensive
-------------------------
None
>

我的最终目标是让印刷品显示在虚线下。如果您想知道,这是assets.player文件中的print语句。

def show_player_stats(self, category):
#if the input for category is put into all upper case and it says "OFFENSIVE", do this
if category.upper() == "OFFENSIVE":
#print the stats. {} means a filler, and the .format makes it print the value based off the variables, in order; strength: {} will print strength: 15 if strength = 15
print("Strength: {}nAgility: {}nCritical Chance: {}nSpell Power: {}nIntellect: {}nSpeed: {}".format(self.strength, self.agility, self.criticalChance, self.spellPower, self.intellect, self.speed))
#or, if the input for category is put into all upper case and it says "DEFENSIVE", do this
elif category.upper() == "DEFENSIVE":
#same as before
print("Health: {}/{}nStamina: {}nArmor: {}nResilience: {}".format(self.currentHealth, self.maxHealth, self.stamina, self.armor, self.resil))
elif category.upper() == "INFO":
print("Name: {} {}nGold: {}nClass: {}nClass Description: {}".format(self.first_name, self.last_name, self.gold, self.class_, self.desc))
#if its anything else
else:
#raise an error, formating the Category {} with the category input given
raise KeyError("Category {} is not a valid category! Please choose Offensive or Defensive.".format(category))

我是不是错过了什么?我不知道我做错了什么。

这是L3viathan说的。您的show_player_stats将打印而不是返回。因此,您的format语句正在正确打印所有内容,它只是在show_player_stats打印其输出后才打印出来。

最新更新