Reputation: 33
Whenever I try to run this code, I get an error about str()
and int()
:
current_year = input ("What year is it?")
current_year = int(current_year)
birth_year = input ("What year were you born in?")
birth_year = int(birth_year)
print ("You are") + (current_year - birth_year) + ("years old.")
How can I get this code to work?
Any help would be greatly appreciated!
Upvotes: 2
Views: 1133
Reputation: 233
Nick I see you are a 11 yo entrant - keep up the enthusiasm and come here to us for answers, but do your HW first.
Strings str() are basically long texts. So if you want to concatenate (join back to back) with other texts, you have to first convert the numbers into text. Hence str (1972 -1960) will give you 12 as a text string. Once it is in that form, mathematical operations on it will return an error or null, but str(current_year - birth_year) + " years old." will give you " 12 years old." - with a 'space' factored in.
Upvotes: 0
Reputation: 8149
Try casting your integers to strings with python's built-in str()
method then just add the appropriate string concatenations like so:
print("You are " + str(current_year - birth_year) + " years old.")
Hopefully that helps!
Upvotes: 1
Reputation: 31
Add str(number) to your print statement.
print ("You are " + str(current_year - birth_year) + " years old.")
Upvotes: 1