With the quotations, character_age is a string. Without the quotations, character_age is a float (floating-point number). When concatenating values (think: adding, combining, summing, etc.), a general rule of thumb is that only like values can be concatenated. Thus, when running this line:
print("he was " + character_age + " years old")
an exception will be raised if character_age is not a string.
If you want to run the code with character_age as a float, here are a few possible solutions (there may be, and probably are, more):
- Convert
character_age to a string
This would involve calling the Python built-in str() function and passing character_age. This will return the float stored in character_age in its string representation:
character_age = 50.86798
...
print("he was " + str(character_age) + " years old")
- Use string formatting to format
character_age into the string
Use whichever style of string formatting you like best, whether it be C-style string formatting, the str.fomat() method, or f-strings:
character_age = 50.86798
...
print("he was %s years old" % character_age)
print("he was {} years old".format(character_age))
print(f"he was {character_age} years old")