小编use*_*227的帖子

如何使python代码运行更多的单词

代码只运行一个单词而不是整个字符串

def silly_case(in_string):
    firstlet =  in_string[0]
    firstlet = firstlet.lower()
    upperpart =  in_string[1:]
    upperpart = upperpart.upper()
    in_string =  firstlet + upperpart
    return in_string

silly_string = silly_case("This is a string")
print(silly_string)
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
59
查看次数

使用 functools.total_ordering 进行比较

我有下面的代码...

class BankAccount:
    """ Simple BankAccount class """

    def __init__(self, balance=0):
        """Initialize account with balance"""
        self.balance = balance

    def deposit(self, amount):
        """Deposit amount to this account"""
        self.balance += amount

    def withdraw(self, amount):
        """Withdraw amount from this account"""
        self.balance -= amount

    def __str__(self):
        return 'Account with a balance of {}'.format(self.balance)

    def __repr__(self):
        return "BankAccount(balance={})".format(self.balance)

    def __bool__(self):
        if self.balance > 0:
            return True
        else:
            return False
Run Code Online (Sandbox Code Playgroud)

该代码基本上是一个简单的银行账户模拟器。我想实现对 BankAccount 对象的比较,以便可以根据它们的余额来比较实例。我想使用 functools.total_ordering 来做到这一点。预期输出低于...

    account1 = BankAccount(100)
    account2 = BankAccount()
    account3 = BankAccount(100)
    account1 == …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
1890
查看次数

标签 统计

python ×2

python-3.x ×2