Получение другого выхода, когда я использую строку F в python - PullRequest
2 голосов
/ 07 апреля 2020

    #Make a class that represents a bank account. Create four methods named set_details, display, withdraw and deposit.
    #     In the set_details method, create two instance variables : name and balance. The default value for balance should
    #     be
    # zero. In the display method, display the values of these two instance variables.
    #
    #     Both the methods withdraw and deposit have amount as parameter. Inside withdraw, subtract the amount from balance
    # and inside deposit, add the amount to the balance.
    #
       # Create two instances of this class and call the methods on those instances.
    class bank:

        def set_details(self, name, balance=0):
            self.name = name,
            self.balance = balance,


        def display(self):
            print(f"name = {self.name}. Balance = {self.balance}"),



        def withdraw(self, a):
            self.balance -= a,
            print(f"Balance after withdrawn {self.balance}")


        def deposite(self, b):
            self.balance += b,
            print(f"Balance after deposite {self.balance}")

    ankit = bank()
    ankit.set_details("ankit", "2300")
    ankit.display()

output

(venv) C: \ Users \ admin \ PycharmProjects \ ankitt> bank.py name = ('ankit',). Баланс = ('2300',)

выходное имя name = ankit. баланс = 2300

почему круглые скобки появляются с кавычками и запятыми

1 Ответ

4 голосов
/ 07 апреля 2020
    def set_details(self, name, balance=0):
            self.name = name,
            self.balance = balance,

Удалить запятые после обоих назначений. Наличие там запятых присваивает им tuples, а не их фактический тип.

Кроме того, как упоминал в комментариях Марсель Уилсон, также удалите запятые из withdraw и deposite.

        def withdraw(self, a):
            self.balance -= a,
            print(f"Balance after withdrawn {self.balance}")


        def deposite(self, b):
            self.balance += b,
            print(f"Balance after deposite {self.balance}")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...