Python - 如果布尔值小于某个数字,如何替换?

Python - How to replace boolean value if less than a certain number?

正在创建我自己的对象和 类,我正在尝试编写一个方法来检查对象中的数字,如果发现数字小于 50,它将用另一个布尔值替换具有预定义值 False 的对象。

目前我有一个 if/else 语句来检查 gamePrice 的值是否小于 50,如果是,它应该将 isASteal 的值更改为 True。如果大于 50,则应更改为 False。 isASteal 的默认值设置为 False,gamePrice 的默认值是 0

class videoGames(object):
def __init__(self, gameName = '', gamePrice = 0, isASteal = False):
    self.gameName = gameName
    self.gamePrice = gamePrice
    self.isASteal = isASteal

def gameValue(self):
    if self.gamePrice == 0 or self.gamePrice >= 50:
        self.isASteal = False

    else:
        self.isASteal = True
    fullGames = 'Title:{}\t\ Price: ${}\t\ Steal: {}'.format(self.gameName, self.gamePrice, self.isASteal)

    return fullGames

如果用户通过以下语句调用函数:

    game1 = videoGames('Call of Duty', 15)

他们应该得到如下所示的输出:

     Title: Call of Duty      Price:            Steal: True

相反,我得到:

     Title: Call of Duty       Price:           Steal: False

如果想在调用实例时打印字符串,可以重写class

的dunder__str__()方法
class videoGames(object):
    def __init__(self, gameName = '', gamePrice = 0, isASteal = False):
        self.gameName = gameName
        self.gamePrice = gamePrice
        self.isASteal = isASteal

    #Overriden ___str__ method
    def __str__(self):
        if self.gamePrice == 0 or self.gamePrice >= 50:
            self.isASteal = False

        else:
            self.isASteal = True
        fullGames = 'Title:{}\t\ Price: ${}\t\ Steal: {}'.format(self.gameName, self.gamePrice, self.isASteal)

        return fullGames

game1 = videoGames('Call of Duty', 15)
print(game1)

输出将是

Title:Call of Duty   Price:   Steal: True