r/code • • Jun 13 '21

Halp pls! How do I fix this?

Post image
15 Upvotes

5 comments sorted by

7

u/xtcylove Jun 13 '21

Use the python format() function.

“your height in centimetres is {}”.format(height_cm)

5

u/[deleted] Jun 13 '21

In addition to format, you apparently have a couple options:

http://net-informations.com/python/err/convert.htm

A TypeError can occur if the type of an object is not what the Python interpreter expected to see. This error is a common mistake made by beginning developers is to use the '+' operator between values of incompatible types. This error message Can't convert 'int' object to str implicitly is clear, when concatenating strings with integers - you can't directly stick together a string and an integer. So, in order to resolve this problem, you have to explicitly parse the integer to a string by the str() built-in function .

Further...

The TypeError is generated because '+' (addition) does not work between an integer and a string. Unlike many other programming languages out there, Python does not implicitly typecast integers (or floats) to strings when concatenating with strings. Fortunately, Python has a built-in function str() which will convert the parameter passed in to a string format.

There are 3 ways to resolve this problem:

print ("Total number of days: " + str(days))

print ("Total number of days: ", days)

print ("Total number of days: {}".format(days))

2

u/42696 Jun 14 '21

I would also add

print(f"Total number of days: {days}")

I love fStrings, I feel like they just look very clean and are very intuitive.

2

u/4abc377878_ Jun 13 '21

it could be bc in python everything you print needs to be in string format, you can try str(height_cm) instead of just the variable. hopefully that works, if not you can always google the error message.