我在第4行的TypeError
上得到了这个print("I'm "+ ageStr +"years old.")
,我已经尝试了f字符串,甚至是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"
答案 0 :(得分:1)
这是您解决错误的方法,
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")
答案 1 :(得分:-1)
要打印时,需要明确将类型int变量转换为str:
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:" + str(answerYears))
answerMonths = answerYears*12
print("In 3 years and 6 months, I'll be " + str(answerMonths) + " months old")
输出:
I'm 24years old.
The total number of years:27
In 3 years and 6 months, I'll be 324 months old