python - 当我在 python 中使用 f 字符串时得到不同的输出
问题描述
#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
为什么圆括号与引号和逗号一起出现
解决方案
def set_details(self, name, balance=0):
self.name = name,
self.balance = balance,
删除两个分配后的逗号。在那里有逗号将它们分配为tuples
而不是它们的实际类型。
此外,正如 Marcel Wilson 在评论中提到的那样,从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}")
推荐阅读
- smartcard - Java Card:发送 T=0 的长响应
- reactjs - antd中选择标签的占位符属性不起作用
- android - 如何使用 ViewTreeObserver.OnGlobalLayoutListener() 从回收器视图项访问图像视图
- sap-ase - 获取 AUTO INCREMENT 的下一个值
- java - 如何将 Python 套接字转换为 Java?Tello DJI 无人机
- variables - 在 go 中声明一个没有值的全局变量
- google-cloud-platform - 我是否需要在 gcloud 中部署功能才能使用 OCR?
- node.js - -print 通过 npm 打印出字符串,但实际上通过 powershell 进行评估
- mysql - MYSQL Group By 值 > 0
- jasper-reports - 无效的 Jasper 报告定义 (JRXML)