eSurfsnake
eSurfsnake

Reputation: 689

Can't convert NonType to String error in Python

I have a small piece of Python code which uses formatting and does not behave as expected.

Here is the code:

TypeError: Can't convert 'NoneType' object to str implicitly

As you can see, I check the type of x and it is a float. I didn't check y in this version, but I have checked and it is a float as well. float as well.

Yet when I do to print them with formatting, Python complains that it can't convert NoneType to string. And, even if I put str() around each format invocation, it still doesn't see all the objects passed to print() as strings.

Shouldn't the formatting take two floats and deliver a (formatted) string?

Upvotes: -2

Views: 1189

Answers (2)

CristiFati
CristiFati

Reputation: 41206

You misunderstood [Python 3.Docs]: Built-in Functions - print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) syntax. The 2nd one shouldn't be there - as print doesn't return anything (or returns None).

You wanted the line to be:

print("{:.4f}    {:.4f}".format(x, y))

Upvotes: 0

Oliver Robie
Oliver Robie

Reputation: 976

I think your mistake may be that additional print("{:.4f}").format(y)
Try replacing it with something like:
print("{:.4f}".format(x) + " " + "{:.4f}".format(y))

Upvotes: 1

Related Questions