python - TypeError:字符串对象不可调用
问题描述
我在第 4 行得到了这个TypeError
。print("I'm "+ ageStr +"years old.")
我已经尝试过 f-strings,甚至str(ageStr) / str(age)
- 我在第 4 行仍然遇到同样的错误。
ageStr = "24" #I'm 24 years old.
age = int(ageStr)
print("I'm "+ ageStr +"years old.")
three = "3"
answerYears = age + int(three)
print("The total number of years:" + "answerYears")
answerMonths = answerYears*12
print("In 3 years and 6 months, I'll be " + answerMonths + " months old"
解决方案
这是您的错误的解决方案,
print("In 3 years and 6 months, I'll be " + str(answerMonths) + " months old")
第二种解决方案是简单地删除字符串连接,
print("In 3 years and 6 months, I'll be ", answerMonths, " months old")
推荐阅读
- flutter - 如何在颤动中制作可扩展的卡片堆栈
- intellij-idea - IntelliJ 在使用 ZSH 时没有拾取环境变量?
- java - 我需要帮助让我的输入按钮在用鼠标单击时工作
- flutter - 如何在 Flutter 中构造表格数据
- python-3.x - UNET 训练:准确度从 0.99 开始
- c# - 使用 NPOI 从 Excel 中的单元格获取完整的日期和时间值
- r - 如何对来自多个数据框的数据进行分组
- tensorflow - 使用 model.fit 训练时列出超出范围的索引
- java - Android Studio 位置通知已更改服务
- sql - Oracle:错误 ORA-02270(此列列表没有匹配的唯一键或主键)