Python3 使用 .format() 的打印语句反向打印

Python3 Print statement using .format() prints in reverse

我正在尝试在 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.")

测试函数如下

编辑:基本字符不同,因为我现在正在测试一种新的打印格式,还没有编辑其他 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: Offensive\n-------------------------\n{}'.format(player1.show_player_stats("offensive")))
    #. . .
    elif args == "ARCHER_OFFENSE":
        print('Archer Character: Offensive\n-------------------------\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 文件中的打印语句。

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 打印而不是 returns。所以你的格式声明正确地打印了所有内容,它只是在 show_player_stats 打印它的输出之后被打印出来。