首页 > 解决方案 > TypeError:字符串对象不可调用

问题描述

我在第 4 行得到了这个TypeErrorprint("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"

标签: python

解决方案


这是您的错误的解决方案,

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")   

推荐阅读