简体   繁体   English

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

[英]Python3 Print statement using .format() prints in reverse

I am trying to create a simple terminal-based game in Python 3. I'm using cmd module to make a menu, and within it I use a test script. 我试图用Python 3创建一个简单的基于终端的游戏。我使用cmd模块制作菜单,并且在其中使用测试脚本。 Here's the code. 这是代码。

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.")

The test function goes as follows. 测试功能如下。

edit: the basecharacter is different because I'm just now testing a new print format, didn't edit the other if statements yet 编辑:基本字符是不同的,因为我现在正在测试一种新的打印格式,还没有编辑其他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
    #. . .

It should print Archer Character: Offensive , followed by a line, followed by the formatted code. 它应打印Archer Character: Offensive ,后跟一行,然后是格式化代码。 But when I print it, this is the terminal output. 但是当我打印它时,这是终端输出。

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
>

My end goal is for the print to show under the dashed line. 我的最终目标是使打印件显示在虚线下方。 If you're wondering, this is the print statement in the assets.player file. 如果您想知道,这是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))

Am I missing something? 我想念什么吗? I don't know what I'm doing wrong. 我不知道我在做什么错。

It's what L3viathan said. 这就是L3viathan所说的。 Your show_player_stats prints instead of returns. 您的show_player_stats打印而不是退货。 So your format statement is printing everything correctly, it just gets printed after show_player_stats prints its output. 因此,您的format语句可以正确打印所有内容,仅在show_player_stats打印输出后才打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM