当我在python中使用f字符串时获得不同的输出

问题描述 投票:0回答:2

    #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()

输出

(venv)C:\ Users \ admin \ PycharmProjects \ ankitt> bank.py名称=('ankit',)。余额=('2300',)

想要的输出名称= ankit。余额= 2300

为什么在圆括号中出现引号和逗号

python oop pycharm f-string
2个回答
1
投票
    def set_details(self, name, balance=0):
            self.name = name,
            self.balance = balance,

在两个作业后都删除逗号。逗号在此处分配为tuples,而不是其实际类型。


0
投票

您的余额是一个字符串。

ankit.set_details("ankit", "2300")

您可以在此处将其设置为整数

ankit.set_details("ankit", 2300)

或这里

print(f"name = {self.name}. Balance = {int(self.balance)}")

取决于您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.