简体   繁体   English

打印(object.method)不打印预期的输出?

[英]print(object.method) not printing expected output?

I'm having a foray into OOP in python, 我正在尝试使用python进行OOP,

This project is creating some randomly generated RPG characters 该项目正在创建一些随机生成的RPG角色

The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats. 我遇到的问题是我正在创建这些随机生成的字符的列表,并希望在那里打印统计数据。

Here is how the characters are randomly generated: 这是随机生成字符的方式:

def generateCharacters():
    classes = ["B", "E", "W", "D", "K"]

    choice = random.choice(classes)

    if choice == "B":
        return barbarian(70, 20, 50)
    elif choice == "E":
        return elf(30, 60, 10)
    elif choice == "W":
        return wizard(50, 70, 30)
    elif choice == "D":
        return dragon(90, 40, 50)
    elif choice == "K":
        return knight(60, 10, 60)

and here is the barbarian class, all the other classes are more or less identical: 这是野蛮阶级,所有其他阶级或多或少是相同的:

class barbarian(character):
    def __init__(self, charPower, charSAttackPwr, charSpeed):
        # Getting the properties from the inheritted character Base Class
        character.__init__(self, "B", 100)
        self.power = charPower
        self.sAttackPwr = charSAttackPwr
        self.speed = charSpeed

    # Method for getting and returning all the stats of the character
    def getStats(self):
        # Creating a string to hold all the stats, using concatenation
        stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack 
        Power: %s, Speed: %s" % (self.name, self.type, self.health, 
        self.power, self.sAttackPwr, self.speed)
        # Returns stats to the the function that called
        return stats

I've created a method called getStats, which using string concatenation to make a string that shows all the stats: 我创建了一个名为getStats的方法,该方法使用字符串连接来创建一个显示所有统计信息的字符串:

# Method for getting and returning all the stats of the character
def getStats(self):
    # Creating a string to hold all the stats, using concatenation
    stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
    # Returns stats to the the function that called
    return stats

When I run the code, it calls main(), which in turn calls menu(): 当我运行代码时,它调用main(),而后者又调用menu():

def menu(gameChars):
    print("Welcome to the RPG Character Simulator")
    print("Here is your randomly generated team: ")
    for x in gameChars:
        print(x.getStats)


def main():
    gameChars = []

    for x in range(10):
        y = generateCharacters()
        gameChars.insert(x, y)

    #z = generateCharacters()
    menu(gameChars)
    #print(z.getStats)

The output I was expecting from the print(x.getStats) would've been, using examples: 使用示例,我期望从print(x.getStats)获得的输出将是:

Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20

but instead, I get this: 但是,我得到了这个:

<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>

What am I missing with this? 我想念什么? and how could I get the intended output? 以及如何获得预期的输出?

Thanks in advance for any help 预先感谢您的任何帮助

Replace this : 替换为:

print(x.getStats)

With this : 有了这个 :

print(x.getStats())

another version would be to use the @property decorator: 另一个版本是使用@property装饰器:

class Barbarian(Character):
  @property
  def getStats(self):
    return 'Name: {.name}'.format(self)

which would allow: 这将允许:

bob = Barbarian('bob', …)
print(bob.getStats)

to work as you expect 像您期望的那样工作

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

相关问题 将Object.method()的“ method()”部分作为参数传递 - Pass the “method()” portion of Object.method() as a parameter @ object.method在Python中做什么? - What does @object.method do in Python? Python中的object.method()和method(object)之间的区别? - Difference between object.method() and method(object) in Python? Python 中的 module.function() 与 object.method()。 他们是一样的吗? - module.function() vs object.method() in Python. Are they the same? 调用 object.method() 和 Class.method(object) 时,幕后发生了什么? - What is happening behind the scenes when calling object.method() and Class.method(object)? friendCount 未打印预期输出 - friendCount not printing expected output Django Wagtail:使用自己的object.method而不是重写object.get_context方法访问数据是否不好? - Django Wagtail: Is it bad practise access data using own object.method instead of rewriting object.get_context method? 打印python对象不会产生预期的&lt;__ main __。myclass…&gt;输出 - Printing a python object doesn't yield the expected <__main__.myclass …> output 在 pygame 中打印 keydown 事件时的预期输出 - Expected output when printing keydown events in pygame 为什么我的 print() 输出打印在两行上? - Why is my print() output printing on two lines?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM