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. 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.
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. 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.
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. Your show_player_stats prints instead of returns. So your format statement is printing everything correctly, it just gets printed after show_player_stats prints its output.