1

I'm getting this TypeError on the print("I'm "+ ageStr +"years old."), on line 4. I've tried f-strings and even str(ageStr) / str(age) - I'm still getting same error on line 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"
Masi
  • 19
  • 2
  • 9
  • 4
    What have you stored in `print`? Can't ask you what is the output of `print(print)` also. – Austin Apr 02 '20 at 14:52
  • 1
    Your code by itself does not exhibit the error. Somewhere in your code *not shown*, you have assigned a string to ``print``, e.g. ``print = "24"``. Note that once you fix this, the last line will throw an error since you cannot concatenate strings and integers - use ``str(answerMonths)`` instead. – MisterMiyagi Apr 02 '20 at 14:57
  • I think you have not put the closing bracket after print in line 11 ? – Sajan Apr 02 '20 at 14:57
  • You don't need to explicitly store a number in it's string type and integer type in different variables. You can simply convert an integer into string object by `str(integer)` and a string object to integer object using `int(string)` – mohammed wazeem Apr 02 '20 at 15:04
  • what about line 4 - print("I'm "+ ageStr +"years old.") ? – Masi Apr 02 '20 at 15:19

2 Answers2

1

Here is the solution for your error,

print("In 3 years and 6 months, I'll be " + str(answerMonths) + " months old")   

Second solution is simply remove string concatenation,

print("In 3 years and 6 months, I'll be ", answerMonths, " months old")   
Arpit Maiya
  • 177
  • 7
-1

You need to explicilty type cast int variable into str when you want to print:

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

Ouput:

I'm 24years old.                                                                                                      
The total number of years:27                                                                                          
In 3 years and 6 months, I'll be 324 months old 
Sheri
  • 1,383
  • 3
  • 10
  • 26