我试图用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.")
测试功能如下。
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))
我想念什么吗?我不知道我在做什么错。
答案 0 :(得分:1)
这就是L3viathan所说的。您的show_player_stats打印而不是退货。因此,您的format语句可以正确打印所有内容,只是在show_player_stats打印输出后才打印。